ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def repeat(n):
    print("R", n, end=" ")
    def deco(fn):
        def w(x):
            return [fn(x) for _ in range(n)]
        return w
    return deco
@repeat(2)
def sq(x):
    return x * x
print(sq(3))
  1. A. [9, 9]
  2. B. R 2 [9, 9]
  3. C. R 2 9
  4. D. R 2 [9, 9, 9]

Correct answer: B. R 2 [9, 9]

repeat(2) is called first (printing R 2) and its result deco is applied to sq. Calling sq(3) runs the wrapper, which calls the original twice.

More Python Programming questions

Exams that ask this topic