ProgrammingVery hardProduct company OA
What is printed?
What is printed?
a = [1, 2]; b = a; c = a[:] t = (a, c) a += [3] b = b + [4] c *= 2 print(t[0] is a, b is a, t[1] is c, len(t[0]), len(b), len(t[1]))
- A. True True True 4 4 4
- B. False False True 2 4 4
- C. True False False 3 4 2
- D. True False True 3 4 4
Correct answer: D. True False True 3 4 4
For a list, += and *= call __iadd__/__imul__ and mutate the object in place, so every other reference (the tuple slots) sees the change and identity is preserved: t[0] is a with 3 items, t[1] is c with 4 items. b = b + [4] instead builds a new 4-element list and rebinds b, so b is no longer a, and a stays at 3 items. Output: True False True 3 4 4. Treating b + [4] as in-place gives the all-True option.
More Python Programming questions
- What is printed? python a = list(range(8)) b = a[::2] a[1::2] = b[::-1] a[2:5] = [] del a[-2:] b[0] = 99 print(a, b)
- 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…