ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq
h = []
for x in [5, 2, 8]:
heapq.heappush(h, x)
print(heapq.heappop(h), heapq.heappop(h), h)- A. 5 2 [8]
- B. 2 8 [5]
- C. 2 5 [8]
- D. 2 5 []
Correct answer: C. 2 5 [8]
heappop returns the smallest value each time, so 2 and then 5 come out. The only item left is 8.
More Python Programming questions
- What is printed? python print(sorted([3, 1, 2], key=lambda x: -x)[0], max([3, 1, 2], key=lambda x: -x))
- What is printed? python print([x for x in range(20) if x % 3 == 0 and x % 4 == 0])
- What is printed? python print(len({(i, j) for i in range(3) for j in range(3) if i <= j}))
- What is printed? python print(sum(1 for c in 'Hello World' if c.isupper()), 'Hello World'.count('o'))
- What is printed? python print(float('1e400'), -float('1e400'))
- What is printed? python print(str(1e16), str(1e15), str(123456789.0))