ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def log(prefix):
    def deco(fn):
        def w(*a):
            return prefix + str(fn(*a))
        return w
    return deco
@log("A:")
@log("B:")
def two():
    return 2
print(two())
  1. A. B:A:2
  2. B. A:2
  3. C. B:2
  4. D. A:B:2

Correct answer: D. A:B:2

Decorators apply bottom-up, so two = log('A:')(log('B:')(two)). The outer wrapper (A) calls the inner wrapper, which returns 'B:2', and then prefixes 'A:'.

More Python Programming questions

Exams that ask this topic