← 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 →- Q1. What is the main benefit of with open(...) as f:?
- Q2. Which two methods make an object a context manager?
- Q3. In with Thing() as t:, what is stored in t?
- Q4. What does this print? (code)
- Q5. What happens if exit returns True after an exception in the block?
- Q6. What does this print? (code)
- Q7. In a @contextmanager function, where should the clean-up code go?
- Q8. What does this print? (code)
- Q9. Why is try/finally alone used less often than with for files?
- Q10. What happens if you call f.read() after the with open(...) as f: block has ended?