Context Managers — with, __enter__/__exit__ & contextlib — Concept Check
Questions on Context Managers — with, __enter__/__exit__ & contextlib. The with statement guarantees setup/teardown around a block — files, locks, DB transactions — via __enter__/__exit__ or the @contextmanager generator shortcut.
Questions in this quiz
8 questions on Context Managers — with, __enter__/__exit__ & contextlib — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses a custom context manager class to manage database transactions. If an exception occurs within the with block, what will the __exit__ method do?
- AIt will print 'COMMIT' and suppress the exception.
- BIt will print 'ROLLBACK' and suppress the exception.
- CIt will print 'COMMIT' and re-raise the exception.
- DIt will print 'ROLLBACK' and re-raise the exception.
2.A developer uses @contextmanager to create a temporary state change for an environment variable. What ensures the original state is restored?
- AThe finally block after yield in the generator.
- BThe exception handling within the with block.
- CThe code before yield in the generator.
- DThe __enter__ method of the context manager.
3.What happens when you use a context manager with a generator decorated by @contextmanager and an exception is raised in the block?
- AThe exception is handled by the yield statement.
- BThe exception is swallowed by the context manager.
- CThe exception is propagated after executing the finally block.
- DThe exception is ignored and the program continues.
4.A developer claims that using with guarantees no exceptions will propagate outside the block. Is this true?
- AYes, if using a generator with @contextmanager.
- BNo, exceptions are always suppressed by with.
- CNo, exceptions can propagate if __exit__ returns False.
- DYes, with always handles exceptions internally.
5.A service uses a context manager for file operations. What happens if the file operation within the with block raises an IOError?
- AThe file remains open and the IOError is suppressed.
- BThe file is closed and the IOError is suppressed.
- CThe file is closed and the IOError is re-raised.
- DThe file remains open and the IOError is re-raised.
6.In a context manager implemented with a class, what is the role of the __enter__ method?
- AIt sets up resources and returns an object for use in the block.
- BIt tears down resources after the block executes.
- CIt handles exceptions raised in the block.
- DIt suppresses exceptions raised in the block.
7.A developer uses a context manager to time a block of code. What ensures the timing is accurate even if an exception occurs?
- AThe __enter__ method of the context manager.
- BThe code before yield in the generator.
- CThe exception handling within the with block.
- DThe finally block after yield in the @contextmanager generator.
8.A developer believes that using @contextmanager is less efficient than a custom class because it involves a generator. Is this correct?
- ANo, @contextmanager reduces boilerplate and is efficient.
- BNo, @contextmanager is less efficient due to overhead.
- CYes, generators are always slower than classes.
- DYes, because generators cannot handle exceptions.