ProgrammingVery hardProduct company OA
What is printed?
What is printed?
class Acc:
def __init__(self):
self.items = []
def __iadd__(self, v):
self.items.append(v)
a = Acc()
b = a
a += 1
print(a, len(b.items), b is a)- A. None 1 False
- B. <Acc> 1 True
- C. None 0 False
- D. None 1 True
Correct answer: A. None 1 False
a += 1 calls a.__iadd__(1) and then ASSIGNS its return value back to a. The method mutates the object (items becomes [1]) but forgets to return self, so it returns None and a is rebound to None. b still refers to the original object, which holds one item, and b is a is now False.
More Python Programming questions
- What is printed? python def f(): log = [] try: log.append("t") raise ValueError("v") except ValueError as e: log.append("e") raise…
- What is printed? python it = iter(range(10)) print(5 in it, next(it), 2 in it, list(it), 9 in range(10))
- What is printed? python calls = [] def chk(x): calls.append(x) return x > 2 r1 = any(chk(x) for x in [1, 3, 5]) r2 = all([chk(x) for x in…
- What is printed? python a = [1, 2] b = [a] 3 a = 2 b[0].append(9) print(b[2], len(b))
- What is printed? python a = [1, 2] b = [a] 3 a = a 2 b[0].append(9) print(a, b[1])
- What is printed? python t = ([1],) try: t[0] += [2] except TypeError: pass try: t[0] = t[0] + [3] except TypeError: pass print(t)