ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import heapq
h = [5, 7, 9]
heapq.heapify(h)
r1 = heapq.heappushpop(h, 6)
r2 = heapq.heappushpop(h, 1)
print(r1, r2, sorted(h))
  1. A. 6 1 [5, 7, 9]
  2. B. 5 6 [1, 7, 9]
  3. C. 5 1 [5, 7, 9]
  4. D. 5 1 [6, 7, 9]

Correct answer: D. 5 1 [6, 7, 9]

heappushpop pushes the new item first and then pops the smallest. Pushing 6 returns the old minimum 5 and leaves 6 in the heap, and pushing 1 returns 1 without changing the heap.

More Python Programming questions

Exams that ask this topic