ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

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

heapreplace pops the smallest element first (1 is returned) and then pushes 0. heappushpop does the opposite order, pushing first and then popping, so it would return 0 directly.

More Python Programming questions

Exams that ask this topic