ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
import functools
def deco(fn):
    @functools.wraps(fn)
    def wrapper(*a):
        wrapper.n += 1
        return fn(*a)
    wrapper.n = 0
    return wrapper
@deco
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)
print(fib(5), fib.n, fib.__name__)
  1. A. 5 1 fib
  2. B. 5 15 wrapper
  3. C. 8 15 fib
  4. D. 5 15 fib

Correct answer: D. 5 15 fib

The recursive calls inside fib go through the global name fib, which is now the wrapper, so every call is counted: fib(5) makes 15 calls in total. wraps copies __name__.

More Python Programming questions

Exams that ask this topic