ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
import heapq a = [5, 1, 4] heapq.heapify(a) print([heapq.heappop(a) for _ in range(len(a))], a)
- A. [1, 4, 5] [1]
- B. [1, 4, 5] []
- C. [5, 4, 1] []
- D. [1, 5, 4] []
Correct answer: B. [1, 4, 5] []
Popping every element in a loop that reads len(a) once returns them in sorted order. The list is empty at the end, because the range was computed before the first pop.
More Python Programming questions
- What is printed? python vals = [y for x in range(5) if (y := x x) % 2 == 0] print(vals)
- What is printed? python from functools import partial f = partial(int, base=2) print(f('101'), f('11', base=10))
- What is printed? python print(list(zip([(1, 'a'), (2, 'b')])))
- What is printed? python print(1 if 0 else 2 if 1 else 3, (lambda: 1 if False else 'x')())
- What is printed? python print(type(1/1).name, type(1//1).name, type(2-1).name, type(21).name)
- What is printed? python print(sorted({x % 4 for x in range(10)}))