Question 1 of 8
Quiz
Back to lesson
Quick Check: Python With Statements and Context Managers
Check understanding of with, context-manager protocols, cleanup, narrow exception suppression, caller-owned resources with nullcontext(), and close()-only objects with closing().
8 questions
~5 min
Knowledge check
Quick Check: Python With Statements and Context Managers
Check understanding of with, context-manager protocols, cleanup, narrow exception suppression, caller-owned resources with nullcontext(), and close()-only objects with closing().
- Stuck? Each question has an optional hint.
- Skip a hard question and review it before your results.
- Questions and answer options can shuffle between attempts.
You can retake this quiz whenever you like.
0:00
This follows Python's context-manager protocol described in the lesson.
Review this lesson
Question 2 of 8
In `with Session() as state:`, what supplies the value assigned to state?
This follows Python's context-manager protocol described in the lesson.
Review this lesson
Question 3 of 8
What happens to __exit__ when an exception is raised inside a successfully entered with block?
This follows Python's context-manager protocol described in the lesson.
Review this lesson
Question 4 of 8
When does __exit__ suppress an exception from the with block?
This follows Python's context-manager protocol described in the lesson.
Review this lesson
Question 5 of 8
How do multiple context managers in one with statement behave?
This follows Python's context-manager protocol described in the lesson.
Review this lesson
Question 6 of 8
What does `with suppress(KeyError):` do if a KeyError is raised inside its with block?
contextlib.suppress() suppresses only the exception types explicitly listed in the call. Exceptions not listed still propagate.
Review this lesson
Question 7 of 8
A function receives a stream that belongs to its caller. Which choice lets the function use with without taking responsibility for closing that stream?
nullcontext(stream) returns the same object from __enter__ and performs no cleanup when the with block exits, so ownership remains with the caller.
Review this lesson
Question 8 of 8
A third-party reader has close() but no __enter__() or __exit__(). Which context manager ensures close() runs when the with block exits?
contextlib.closing(reader) adapts an object that exposes close() but not the context-manager protocol. It calls close() when the block exits, even if the body raises; it does not suppress the exception.
Review this lesson
Quiz results
0/0
Review
-
Which method runs when a with statement successfully enters a context?Hint used
-
In `with Session() as state:`, what supplies the value assigned to state?Hint used
-
What happens to __exit__ when an exception is raised inside a successfully entered with block?Hint used
-
When does __exit__ suppress an exception from the with block?Hint used
-
How do multiple context managers in one with statement behave?Hint used
-
What does `with suppress(KeyError):` do if a KeyError is raised inside its with block?Hint used
-
A function receives a stream that belongs to its caller. Which choice lets the function use with without taking responsibility for closing that stream?Hint used
-
A third-party reader has close() but no __enter__() or __exit__(). Which context manager ensures close() runs when the with block exits?Hint used