0
0
Pythonprogramming~20 mins

Best practices for resource management in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Resource Management Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of using context manager with file handling
What is the output of this Python code snippet?
Python
with open('test.txt', 'w') as f:
    f.write('Hello')
print(f.closed)
ATrue
BFalse
CRaises a NameError
DNone
Attempts:
2 left
💡 Hint
Think about what happens to the file after the with block ends.
🧠 Conceptual
intermediate
2:00remaining
Why use 'with' statement for resource management?
Which of the following best explains why using the 'with' statement is recommended for resource management in Python?
AIt automatically handles opening and closing resources, even if errors occur.
BIt makes the code run faster by caching resources.
CIt allows resources to stay open indefinitely for reuse.
DIt disables error messages related to resource usage.
Attempts:
2 left
💡 Hint
Think about what happens if an error occurs inside a 'with' block.
🔧 Debug
advanced
2:00remaining
Identify the resource leak in this code
What is the main problem with this code snippet regarding resource management?
Python
def read_file(filename):
    f = open(filename, 'r')
    data = f.read()
    return data

content = read_file('data.txt')
AThe file is opened in write mode instead of read mode.
BThe file is closed too early before reading.
CThe file is never closed, causing a resource leak.
DThe function returns None instead of file content.
Attempts:
2 left
💡 Hint
Think about what happens to the file after reading.
📝 Syntax
advanced
2:00remaining
Correct syntax for custom context manager
Which option correctly defines a custom context manager class in Python?
Python
class MyResource:
    def __enter__(self):
        print('Resource acquired')
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('Resource released')
A
class MyResource:
    def enter(self):
        return self
    def exit(self, exc_type, exc_val, exc_tb):
        pass
B
class MyResource:
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        pass
C
class MyResource:
    def __enter__(self):
        return self
    def __exit__(self):
        pass
D
class MyResource:
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val):
        pass
Attempts:
2 left
💡 Hint
Check the method names and parameters required for context managers.
🚀 Application
expert
3:00remaining
Using contextlib to manage resources
Which option correctly uses the contextlib module to create a context manager that prints messages on enter and exit?
Python
from contextlib import contextmanager

@contextmanager
def managed_resource():
    print('Start')
    yield
    print('End')
A
with managed_resource():
    print('Inside')

# Output:
# Start
# End
# Inside
B
with managed_resource:
    print('Inside')

# Output:
# Start
# Inside
# End
C
with managed_resource():
    print('Inside')

# Output:
# Inside
# Start
# End
D
with managed_resource():
    print('Inside')

# Output:
# Start
# Inside
# End
Attempts:
2 left
💡 Hint
Remember to call the function to get the context manager object.