ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
def deco2(f):
    print("decorating", f.__name__)
    def wrapper(*a, **kw):
        return f(*a, **kw)
    return wrapper

@deco2
def foo():
    pass

print("done")
  1. A. done decorating foo
  2. B. decorating foo done
  3. C. decorating wrapper done
  4. D. done

Correct answer: B. decorating foo done

The decorator runs immediately when the def is executed (at import/definition time), not when foo is later called, so 'decorating foo' prints before 'done'.

More Python Programming questions

Exams that ask this topic