ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import heapq
h = [1, 3, 5]
r = heapq.heappushpop(h, 0)
print(r, sorted(h))
  1. A. 1 [0, 3, 5]
  2. B. 0 [0, 1, 3, 5]
  3. C. 1 [3, 5]
  4. D. 0 [1, 3, 5]

Correct answer: D. 0 [1, 3, 5]

heappushpop pushes first and then pops. The pushed item 0 is smaller than the heap's minimum, so it is returned at once and the heap is not changed.

More Python Programming questions

Exams that ask this topic