ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
def f(x, lst=[]):
    lst.append(x)
    return lst

a = f(1)
b = f(2, [])
c = f(3)
print(a, b, c)
  1. A. [1] [2] [1, 3]
  2. B. [1, 3] [2] [3]
  3. C. [1] [2] [3]
  4. D. [1, 3] [2] [1, 3]

Correct answer: D. [1, 3] [2] [1, 3]

a and c are the SAME default list object, which holds [1, 3] once both calls have run. b got its own new list [2]. The print happens after all three calls, so a also shows [1, 3].

More Python Programming questions

Exams that ask this topic