ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
def f(x, lst=[]):
lst.append(x)
return lst
f(1)
f(2)
print(f(3))- A. [3]
- B. [1, 2, 3]
- C. [1, 3]
- D. [2, 3]
Correct answer: B. [1, 2, 3]
The default list is created once, when the def runs, and the same list is reused on every call that omits lst. After f(1) and f(2) it holds [1, 2], and f(3) appends to it, giving [1, 2, 3].
More Python Programming questions
- What is printed? python x = 10 def f(): x += 1 return x try: f() except Exception as e: print(type(e).name)
- What is printed? python g = (i i for i in range(4)) print(sum(g), sum(g))
- What is printed? python print({1, 2, 3} ^ {2, 3, 4}, {1, 2} <= {1, 2, 3})
- What is printed? python a = "abc" b = a a += "d" print(a, b)
- What is printed? python t = (1, [2, 3]) t[1].append(4) print(t)
- What is printed? python print(0.1 + 0.2 == 0.3, abs(0.1 + 0.2 - 0.3) < 1e-9)