ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: B. 0 7 [1, 4, 5]

Calling heappushpop on an empty heap returns the pushed item. Here h is not a heap, but the 0 is smaller than the first element of the list, so it is returned at once and h is not touched.

More Python Programming questions

Exams that ask this topic