ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import heapq
h = [(2, 'x'), (1, 'y'), (1, 'a')]
heapq.heapify(h)
heapq.heappush(h, (0, 'z'))
print(heapq.heappop(h), heapq.heappop(h))
  1. A. (0, 'z') (1, 'y')
  2. B. (1, 'a') (0, 'z')
  3. C. (1, 'y') (1, 'a')
  4. D. (0, 'z') (1, 'a')

Correct answer: D. (0, 'z') (1, 'a')

After heapify and the push, the smallest tuple is (0, 'z'). The next smallest is (1, 'a'), since 'a' sorts before 'y' when the first fields tie.

More Python Programming questions

Exams that ask this topic