ProgrammingHardCognizant GenC · TCS NQT

What is printed?

What is printed?
import heapq
a = [5, 1, 4]
heapq.heapify(a)
print([heapq.heappop(a) for _ in range(len(a))], a)
  1. A. [1, 4, 5] [1]
  2. B. [1, 4, 5] []
  3. C. [5, 4, 1] []
  4. D. [1, 5, 4] []

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

Popping every element in a loop that reads len(a) once returns them in sorted order. The list is empty at the end, because the range was computed before the first pop.

More Python Programming questions

Exams that ask this topic