🎙 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 4 of 10

What does this print? (code)

What does this print?

class C:
    def __enter__(self):
        print("in")
        return self
    def __exit__(self, *a):
        print("out")
with C():
    print("body")
  1. A.in out body
  2. B.in body out
  3. C.body in out
  4. D.in body
Answer & explanation

Correct answer: in body out

enter runs first, then the body, then exit.

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 →