ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
import bisect
a = sorted([5, 1, 9, 5, 3, 7, 5])
lo = bisect.bisect_left(a, 3)
hi = bisect.bisect_right(a, 7)
print(a, hi - lo)
  1. A. [1, 3, 5, 5, 5, 7, 9] 4
  2. B. [1, 3, 5, 5, 5, 7, 9] 6
  3. C. [1, 3, 5, 5, 5, 7, 9] 5
  4. D. [1, 3, 5, 5, 5, 7, 9] 3

Correct answer: C. [1, 3, 5, 5, 5, 7, 9] 5

bisect_left(3) is 1 and bisect_right(7) is 6, so the difference 5 counts the values 3, 5, 5, 5 and 7 that lie in the range [3, 7].

More Python Programming questions

Exams that ask this topic