🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Context Managers: all 10 questions

Quiz: Context Managers · Question 8 of 10

What does this print? (code)

What does this print?

from contextlib import contextmanager
@contextmanager
def tag(name):
    print(f"<{name}>", end="")
    yield
    print(f"</{name}>", end="")
with tag("p"):
    print("hi", end="")
  1. A.hi<p></p>
  2. B.<p></p>hi
  3. C.<p>hi
  4. D.<p>hi</p>
Answer & explanation

Correct answer: <p>hi</p>

Setup runs before yield, then the body, then the code after yield.

Can you get the rest right without peeking?

Take the timed Quiz: Context Managers quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →