Challenge - 5 Problems
Master of Handling Multiple Resources
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested with statements
What is the output of this Python code that opens two files using nested
with statements?Python
with open('file1.txt', 'w') as f1: f1.write('Hello') with open('file2.txt', 'w') as f2: f2.write('World') with open('file1.txt', 'r') as f1: with open('file2.txt', 'r') as f2: print(f1.read() + ' ' + f2.read())
Attempts:
2 left
💡 Hint
Remember that the files are opened and read in the order of the nested with statements.
✗ Incorrect
The code writes 'Hello' to file1.txt and 'World' to file2.txt. Then it opens file1.txt and file2.txt for reading in nested with statements and prints their contents separated by a space, resulting in 'Hello World'.
❓ Predict Output
intermediate2:00remaining
Using multiple context managers in one with statement
What will this code print when opening two files in a single
with statement?Python
with open('file1.txt', 'w') as f1, open('file2.txt', 'w') as f2: f1.write('Data1') f2.write('Data2') with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2: print(f1.read() + '-' + f2.read())
Attempts:
2 left
💡 Hint
Multiple context managers can be opened in one with statement separated by commas.
✗ Incorrect
Both files are opened simultaneously in one with statement. The contents are read and joined with a dash, printing 'Data1-Data2'.
🔧 Debug
advanced2:00remaining
Identify the error in resource handling
What error does this code raise when trying to open two files using nested with statements incorrectly?
Python
with open('file1.txt', 'r') as f1: with open('file2.txt', 'r') as f2: print(f1.read() + f2.read())
Attempts:
2 left
💡 Hint
Check the syntax of the with statement lines carefully.
✗ Incorrect
The second with statement is missing a colon at the end, causing a SyntaxError.
❓ Predict Output
advanced2:00remaining
Effect of exception inside multiple with statement
What will be printed by this code that opens two files and raises an exception inside the with block?
Python
try: with open('file1.txt', 'w') as f1, open('file2.txt', 'w') as f2: f1.write('Test') raise ValueError('Oops') f2.write('Fail') except ValueError as e: print('Caught:', e) with open('file1.txt', 'r') as f1: print(f1.read())
Attempts:
2 left
💡 Hint
The exception stops execution inside the with block but the file is still written and closed properly.
✗ Incorrect
The exception is caught and printed. The first file was written before the exception, so reading it prints 'Test'.
🧠 Conceptual
expert2:00remaining
Order of resource release in multiple context managers
When using multiple context managers in one with statement like
with A() as a, B() as b:, in which order are the resources released (their __exit__ methods called)?Attempts:
2 left
💡 Hint
Think about how nested with statements work and the stack of context managers.
✗ Incorrect
Python calls __exit__ methods in reverse order of __enter__ calls. So B's __exit__ is called before A's __exit__.