ProgrammingModerateProduct-company MCQ pattern
Which statement about the heapq module is correct?
- A. heapq implements a max-heap, so heap[0] is the largest item
- B. heapq.heappop returns the largest item in the heap
- C. heapq implements a min-heap, so heap[0] is the smallest item
- D. heapq keeps the whole list sorted after every push
Correct answer: C. heapq implements a min-heap, so heap[0] is the smallest item
heapq maintains the heap invariant, so the smallest item is always at index 0 and heappop removes it. The list is not fully sorted, and heappop returns the smallest item.
More Python Programming questions
- What is printed? python print(sum(range(1, 10 5 + 1, 7)))
- What is printed? python print(pow(7, 222, 13), pow(7, 6, 13))
- What is printed? python print(max(map(len, ['ab', 'cde', 'f'])), sorted([[2, 1], [1, 5], [1, 2]]))
- What is printed? python print(list(enumerate('ab', start=5)))
- What is printed? python print(int(3.99999999999999999), int(-0.9))
- What is printed? python print('{0}{1}{0}'.format('a', 'b'), '{:>3}'.format(7), '%%%d' % 5)