ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def memo(fn):
    cache = {}
    def w(n):
        if n not in cache:
            cache[n] = fn(n)
        return cache[n]
    return w
@memo
def f(n):
    print(n, end=" ")
    return n * 2
f(3); f(3); f(4)
print(f(3))
  1. A. 3 3 4 6
  2. B. 3 4 3 6
  3. C. 3 4 6
  4. D. 3 4 8

Correct answer: C. 3 4 6

The first f(3) computes and prints 3; the second is a cache hit; f(4) prints 4. The final f(3) is a cache hit too, returning 6 without printing.

More Python Programming questions

Exams that ask this topic