0
0
Pythonprogramming~20 mins

Automatic resource cleanup in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Automatic Resource Cleanup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
Atrue
Bfalse
CRaises a NameError
Dnull
Attempts:
2 left
💡 Hint
Think about what the context manager does after the block finishes.
Predict Output
intermediate
2: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')
AResource acquired\nInside with block\nResource released\nException caught
BResource acquired\nResource released\nInside with block\nException caught
CResource acquired\nInside with block\nException caught
DInside with block\nResource acquired\nResource released\nException caught
Attempts:
2 left
💡 Hint
The __exit__ method runs even if an exception happens inside the block.
🔧 Debug
advanced
2: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')
AThe __exit__ method is missing a return statement, so the resource is not released.
BThe __exit__ method should return true to suppress exceptions, but it doesn't.
CThe __exit__ method is defined but does not handle exceptions properly, causing resource leak.
DThe __exit__ method is missing the 'self' parameter, so it is not called.
Attempts:
2 left
💡 Hint
Returning true from __exit__ suppresses exceptions, but not returning anything returns null.
📝 Syntax
advanced
2: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.
A
from contextlib import contextmanager

@contextmanager
def managed():
    print('Start')
    return
    print('End')
B
from contextlib import contextmanager

def managed():
    print('Start')
    yield
    print('End')
C
from contextlib import contextmanager

@contextmanager
def managed():
    print('Start')
    yield
    print('End')
D
from contextlib import contextmanager

@contextmanager
def managed():
    print('Start')
    yield 1
    print('End')
    yield 2
Attempts:
2 left
💡 Hint
The function must be decorated and use exactly one yield to separate setup and cleanup.
🚀 Application
expert
2: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')
A1
B0
C3
D2
Attempts:
2 left
💡 Hint
Each 'with' block calls __enter__ and __exit__ once.