ProgrammingHardCognizant GenC · TCS NQT

What is printed?

What is printed?
import heapq
h = []
for w, name in [(2, 'b'), (1, 'z'), (2, 'a'), (1, 'c')]:
    heapq.heappush(h, (w, name))
out = []
while h:
    out.append(heapq.heappop(h)[1])
print(''.join(out))
  1. A. zcab
  2. B. czba
  3. C. czab
  4. D. zcba

Correct answer: C. czab

Tuples compare element by element, so equal weights are ordered by name. That gives (1,'c') before (1,'z') and (2,'a') before (2,'b').

More Python Programming questions

Exams that ask this topic