ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
import functools
def once(fn):
    done = []
    @functools.wraps(fn)
    def w(*a):
        if not done:
            done.append(fn(*a))
        return done[0]
    return w
@once
def init(v):
    print("init", v, end=" ")
    return v * 3
print(init(2), init(5), init.__wrapped__(1))
  1. A. init 2 6 6 3
  2. B. init 2 init 1 6 6 3
  3. C. init 2 init 5 6 15 3
  4. D. init 2 init 1 6 15 3

Correct answer: B. init 2 init 1 6 6 3

The first call runs init(2) and caches 6; init(5) returns the cached 6 without calling. functools.wraps sets __wrapped__ to the original function, which runs normally (printing init 1). All prints happen before print outputs its arguments.

More Python Programming questions

Exams that ask this topic