ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: B. 1 [0, 3] 0 [0, 3]

heapreplace pops the smallest item first and then pushes, so it returns 1 and leaves [0, 3]. heappushpop pushes 0 and immediately pops it because it is the smallest, so the heap is unchanged.

More Python Programming questions

Exams that ask this topic