ProgrammingVery hardProduct-company MCQ pattern
What is printed?
What is printed?
import heapq h = [5, 1, 3, 1] heapq.heapify(h) print(h[0], heapq.heappop(h), heapq.nsmallest(2, [4, -1, 3]), [heapq.heappop(h) for _ in range(3)])
- A. 1 1 [-1, 3] [3, 5, 1]
- B. 1 1 [-1, 3] [1, 3, 5]
- C. 1 5 [-1, 3] [1, 3, 5]
- D. 1 1 [-1, 4] [1, 3, 5]
Correct answer: B. 1 1 [-1, 3] [1, 3, 5]
heapify puts the smallest value at index 0, and heappop removes one 1 so the next smallest 1 remains. nsmallest returns the two smallest values in ascending order, and the remaining heap pops out in order.
More Python Programming questions
- What is printed? python from functools import reduce print(reduce(lambda a, b: a 10 + b, [1, 2, 3]), reduce(lambda a, b: a - b, [10, 2…
- What is printed? python print(sum(1 for c in 'a1b22c333' if c.isdigit()), ''.join(c for c in 'a1b22c333' if c.isalpha()), int(''.join(c…
- What is printed? python print(str(0.1 3), repr(0.1 3) == '0.30000000000000004', 0.1 3 == float(repr(0.1 3)))
- What is printed? python print(10-1 + 10-1 + 10-1 == 0.3, 1/10 + 1/10 + 1/10 == 0.3, 10-1 == 1/10)
- What is printed? python print(sorted([3, 1, 2], reverse=True) == [3, 2, 1], [3, 1, 2][::-1], sorted('cab')[::-1])
- What is printed? python print(int(str(7) 3) 2 % 10, 111 2, (11 2) 10 + 1)