Challenge - 5 Problems
Automatic Resource Cleanup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code using a context manager?
Consider the following Python code that uses a context manager to open a file and write text. What will be printed when this code runs?
Python
with open('testfile.txt', 'w') as f: f.write('Hello world') print(f.closed)
Attempts:
2 left
💡 Hint
Think about what the context manager does after the block finishes.
✗ Incorrect
The 'with' statement automatically closes the file after the block ends, so f.closed is true.
❓ Predict Output
intermediate2:00remaining
What happens if an exception occurs inside a 'with' block?
What will be the output of this code snippet?
Python
class Resource: def __enter__(self): print('Resource acquired') return self def __exit__(self, exc_type, exc_val, exc_tb): print('Resource released') return false try: with Resource() as r: print('Inside with block') raise ValueError('Oops') except ValueError: print('Exception caught')
Attempts:
2 left
💡 Hint
The __exit__ method runs even if an exception happens inside the block.
✗ Incorrect
The __enter__ method runs first, then the block code. When the exception is raised, __exit__ is called to release the resource, then the exception is caught outside.
🔧 Debug
advanced2:00remaining
Why does this custom context manager fail to release the resource?
This code tries to create a context manager but the resource is not released after the block. What is the bug?
Python
class MyResource: def __enter__(self): print('Acquiring resource') return self def __exit__(self, exc_type, exc_val, exc_tb): print('Releasing resource') with MyResource() as r: print('Using resource')
Attempts:
2 left
💡 Hint
Returning true from __exit__ suppresses exceptions, but not returning anything returns null.
✗ Incorrect
The resource is released because __exit__ is called, but if an exception occurs, it is not suppressed because __exit__ returns null. However, in this code no exception occurs, so resource is released. The question is tricky: the resource is released but the __exit__ method should return true if you want to suppress exceptions. The question states resource is not released, but actually it is. So the bug is that __exit__ does not return true to suppress exceptions if needed.
📝 Syntax
advanced2:00remaining
Which option correctly implements a context manager using 'contextlib'?
Select the code snippet that correctly creates a context manager using the @contextmanager decorator from contextlib.
Attempts:
2 left
💡 Hint
The function must be decorated and use exactly one yield to separate setup and cleanup.
✗ Incorrect
Option C correctly decorates the function and uses yield once to separate the setup and cleanup code. Option C is missing the decorator. Option C uses return instead of yield, which is invalid. Option C has two yields, which is invalid syntax.
🚀 Application
expert2:00remaining
How many times is the resource released in this nested context manager code?
Analyze the following code and determine how many times 'Resource released' is printed.
Python
class Resource: def __enter__(self): print('Resource acquired') return self def __exit__(self, exc_type, exc_val, exc_tb): print('Resource released') with Resource() as r1: with Resource() as r2: print('Inside nested with')
Attempts:
2 left
💡 Hint
Each 'with' block calls __enter__ and __exit__ once.
✗ Incorrect
There are two nested 'with' blocks, each acquiring and releasing a resource once, so 'Resource released' is printed twice.