ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
import heapq h = [5, 1, 4] heapq.heapify(h) heapq.heappush(h, 0) small = heapq.heappop(h) print(small, h[0], heapq.nsmallest(2, [9, 3, 7, 1]), heapq.nlargest(1, ["b", "ab"], key=len))
- A. 0 4 [1, 3] ['ab']
- B. 0 1 [1, 3] ['b']
- C. 0 1 [1, 3] ['ab']
- D. 5 1 [1, 3] ['ab']
Correct answer: C. 0 1 [1, 3] ['ab']
heapq maintains a min-heap: the root h[0] is always the smallest, so popping gives 0 and then 1 is at the root. nlargest with key=len picks the longest string.
More Python Programming questions
- What is printed? python from collections import deque d = deque([1, 2, 3], maxlen=3) d.append(4) d.appendleft(0) d.rotate(1)…
- What is printed? python from collections import namedtuple P = namedtuple("P", "x y", defaults=[0]) p = P(1) q = p.replace(y=5) print(p…
- What is printed? python from dataclasses import dataclass, field @dataclass class Item: name: str tags: list = field(defaultfactory=list)…
- What is printed? python from dataclasses import dataclass @dataclass(frozen=True) class Pt: x: int y: int = 0 p = Pt(1) try: p.x = 5…
- What is printed? python nums = [1, 2, 3, 4, 5, 6] evens = list(filter(lambda n: n % 2 == 0, nums)) total = sum(n n for n in nums if n % 2)…
- What is printed? python def chunks(xs, k): for i in range(0, len(xs), k): yield xs[i:i + k] def flatten(seq): for s in seq: if…