ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
import bisect
a = [10, 20, 20, 30]
print([bisect.bisect_left(a, x) for x in (20, 25, 5, 40)])
  1. A. [1, 2, 0, 4]
  2. B. [1, 3, 0, 4]
  3. C. [2, 3, 0, 4]
  4. D. [1, 3, 0, 3]

Correct answer: B. [1, 3, 0, 4]

bisect_left returns the first index where the value fits, so for 20 it is the first of the two equal values. 25 goes before 30 at index 3, and 40 goes at the end, index 4.

More Python Programming questions

Exams that ask this topic