CS FundamentalsVery hardProduct-company placement · Python

What does this code print (lines joined by ' / ')?

What does this code print (lines joined by ' / ')?
def tag(name):
    def deco(f):
        print(name)
        def w():
            return f'<{name}>' + f() + f'</{name}>'
        return w
    return deco

@tag('b')
@tag('i')
def hi(): return 'hi'

print(hi())
  1. A. b / i / <b><i>hi</i></b>
  2. B. i / b / <i><b>hi</b></i>
  3. C. b / i / <i><b>hi</b></i>
  4. D. i / b / <b><i>hi</i></b>

Correct answer: D. i / b / <b><i>hi</i></b>

Stacked decorators apply bottom-up: tag('i') wraps hi first (printing i), then tag('b') wraps that (printing b). At call time the outer b wrapper runs first, so the output is <b><i>hi</i></b>.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic