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))
  1. A. [3]
  2. B. [1, 2, 3]
  3. C. [1, 3]
  4. 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

Exams that ask this topic