ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: C. 2

After sorting, a is [1, 3, 5, 7, 9]. bisect_right(a, 7) is 4 and bisect_left(a, 4) is 2, so the count of values in [4, 7] is 2, which is the count of the values 5 and 7.

More Python Programming questions

Exams that ask this topic