ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
import heapq
h = [5, 1, 4]
heapq.heapify(h)
heapq.heappush(h, 0)
small = heapq.heappop(h)
print(small, h[0], heapq.nsmallest(2, [9, 3, 7, 1]), heapq.nlargest(1, ["b", "ab"], key=len))
  1. A. 0 4 [1, 3] ['ab']
  2. B. 0 1 [1, 3] ['b']
  3. C. 0 1 [1, 3] ['ab']
  4. D. 5 1 [1, 3] ['ab']

Correct answer: C. 0 1 [1, 3] ['ab']

heapq maintains a min-heap: the root h[0] is always the smallest, so popping gives 0 and then 1 is at the root. nlargest with key=len picks the longest string.

More Python Programming questions

Exams that ask this topic