ProgrammingVery hardProduct company OA
What is printed?
What is printed?
a = list(range(8)) b = a[::2] a[1::2] = b[::-1] a[2:5] = [] del a[-2:] b[0] = 99 print(a, b)
- A. [99, 6, 2] [99, 2, 4, 6]
- B. [0, 6, 2] [99, 2, 4, 6]
- C. [0, 6, 4] [99, 2, 4, 6]
- D. [0, 6, 2, 6] [99, 2, 4, 6]
Correct answer: B. [0, 6, 2] [99, 2, 4, 6]
b = a[::2] is an independent copy [0, 2, 4, 6]. Extended-slice assignment writes its reverse into the odd positions: a = [0, 6, 2, 4, 4, 2, 6, 0]. a[2:5] = [] deletes indices 2, 3, 4 → [0, 6, 2, 6, 0]; del a[-2:] leaves [0, 6, 2]. Changing b[0] afterwards cannot reach a because a slice copies. The option starting with 99 treats the slice as a view (NumPy behaviour, not list behaviour).
More Python Programming questions
- What is printed? python class Meter: unit = "m" def init(self): self.dict["v"] = 1 def getattr(self, name): return name.upper() def…
- What is printed? python def counter(): n = 0 def inc(k=1): nonlocal n n += k return n return inc c1, c2 = counter(), counter() g = (c1(i)…
- What is printed? python def f(): out = [] for i in range(4): try: if i == 1: continue if i == 2: raise KeyError(i) if i == 3: return out…
- What is printed? python rows = [("x", 2), ("y", 1), ("z", 2), ("w", 1), ("v", 3)] a = sorted(rows, key=lambda r: r[1], reverse=True) b =…
- What is printed? python class P: def init(self, x, y): self.x, self.y = x, y def eq(self, o): return self.x == o.x def hash(self): return…
- What is printed? python it = iter(range(10)) a = list(zip(it, "ab")) b = next(it) c = [x for x in it if next(it, 0) % 3] print(a, b, c)