🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← All topics

Quiz: Context Managers: questions with answers

10 questions, each with the answer and a short explanation.

Take the timed quiz + get a certificate →
  1. Q1. What is the main benefit of with open(...) as f:?
  2. Q2. Which two methods make an object a context manager?
  3. Q3. In with Thing() as t:, what is stored in t?
  4. Q4. What does this print? (code)
  5. Q5. What happens if exit returns True after an exception in the block?
  6. Q6. What does this print? (code)
  7. Q7. In a @contextmanager function, where should the clean-up code go?
  8. Q8. What does this print? (code)
  9. Q9. Why is try/finally alone used less often than with for files?
  10. Q10. What happens if you call f.read() after the with open(...) as f: block has ended?