ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
from collections import deque d = deque([1, 2, 3], maxlen=3) d.append(4) d.appendleft(0) d.rotate(1) print(list(d), d.maxlen, d[-1])
- A. [3, 0, 2] 3 2
- B. [2, 3, 0] 3 0
- C. [4, 0, 1] 3 1
- D. [0, 1, 2] 3 2
Correct answer: A. [3, 0, 2] 3 2
With maxlen=3, append(4) drops 1 from the left: [2, 3, 4]. appendleft(0) drops 4 from the right: [0, 2, 3]. rotate(1) moves the last item to the front: [3, 0, 2], whose last element is 2.
More Python Programming questions
- 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…
- What is printed? (Output lines are shown separated by " / ".) python def f(a, b, /, c, , d): return (a, b, c, d) args = (1, 2) kw = {"c"…