ProgrammingVery hardProduct-company MCQ pattern

What is printed?

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

Correct answer: B. 1 1 [-1, 3] [1, 3, 5]

heapify puts the smallest value at index 0, and heappop removes one 1 so the next smallest 1 remains. nsmallest returns the two smallest values in ascending order, and the remaining heap pops out in order.

More Python Programming questions

Exams that ask this topic