ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
def f(a, b={"x": 1}):
b["x"] += a
return b
print(f(1))
print(f(2))- A. {'x': 2} {'x': 3}
- B. {'x': 2} {'x': 4}
- C. {'x': 1} {'x': 2}
- D. {'x': 3} {'x': 3}
Correct answer: B. {'x': 2} {'x': 4}
The default dict b is created once and shared across calls. f(1) adds 1 to x (1->2). f(2) then adds 2 to that SAME dict's now-current value (2->4).
More Python Programming questions
- What is printed? python def f2(x, y, , z=None): if z is None: z = [] z.append(x + y) return z print(f2(1, 2)) print(f2(3, 4))
- What is printed? python a = {1, 2, 3} b = a b.add(4) c = a.copy() c.add(5) print(a, c)
- What is printed? python class Meta: shared = [] def init(self, x): self.shared.append(x) m1 = Meta(1) m2 = Meta(2) print(m1.shared…
- What is printed? python def outer(): data = [1, 2, 3] def modify(): data.append(4) modify() return data print(outer())
- What is printed? python a, b = 1, 2 a, b = b, a + b print(a, b)
- What is printed? python from dataclasses import dataclass @dataclass class Point: x: int y: int = 0 p1 = Point(1, 2) p2 = Point(1, 2)…