ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import heapq
h = []
for x in [5, 2, 8]:
    heapq.heappush(h, x)
print(heapq.heappop(h), heapq.heappop(h), h)
  1. A. 5 2 [8]
  2. B. 2 8 [5]
  3. C. 2 5 [8]
  4. D. 2 5 []

Correct answer: C. 2 5 [8]

heappop returns the smallest value each time, so 2 and then 5 come out. The only item left is 8.

More Python Programming questions

Exams that ask this topic