ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def f(n, seen=[]):
    if n in seen:
        return seen
    seen = seen + [n]
    return seen
print(f(1), f(2), f(1))
  1. A. [1] [1, 2] [1, 2]
  2. B. [1] [1, 2] [1, 2, 1]
  3. C. [1] [2] [1]
  4. D. [1] [2] []

Correct answer: C. [1] [2] [1]

seen = seen + [n] creates a new list each time, so the shared default [] is never mutated; every call starts from the empty default and returns a one-item list.

More Python Programming questions

Exams that ask this topic