ProgrammingHardAccenture pattern
What is printed?
What is printed?
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))- A. [3] [3, 7]
- B. [3] [7]
- C. [3, 7] [3, 7]
- D. [7] [3]
Correct answer: B. [3] [7]
Using the None-sentinel pattern, z gets a FRESH empty list on every call that omits it, avoiding the mutable-default trap; each call therefore returns an independent one-element list.
More Python Programming questions
- 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)…
- What is printed? python x, y = "hello" print(x, y)