ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def f(n, memo={}):
    if n in memo:
        return memo[n]
    r = n if n < 2 else f(n - 1) + f(n - 2)
    memo[n] = r
    return r
f(10)
print(len(f.__defaults__[0]), f(5, {}), len(f.__defaults__[0]))
  1. A. 11 5 6
  2. B. 0 5 0
  3. C. 10 5 10
  4. D. 11 5 11

Correct answer: D. 11 5 11

The mutable default acts as a persistent cache: f(10) stores n = 0..10 (11 keys). Passing an explicit {} for f(5) uses a separate dict, and its recursive calls use the default, already filled, so the default is unchanged.

More Python Programming questions

Exams that ask this topic