Challenge - 5 Problems
With Statement 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 with statement?
Consider this Python code using a custom context manager class. What will it print when run?
Python
class MyContext: def __enter__(self): print('Enter') return 'resource' def __exit__(self, exc_type, exc_val, exc_tb): print('Exit') with MyContext() as res: print('Inside with:', res)
Attempts:
2 left
💡 Hint
Remember, __enter__ runs before the block, __exit__ runs after.
✗ Incorrect
The __enter__ method runs first and prints 'Enter'. Then the block runs printing 'Inside with: resource'. Finally, __exit__ runs printing 'Exit'.
❓ Predict Output
intermediate2:00remaining
What happens if an exception occurs inside the with block?
Look at this code. What will be printed when the exception is raised inside the with block?
Python
class CM: def __enter__(self): print('Start') return self def __exit__(self, exc_type, exc_val, exc_tb): print('End') print('Exception:', exc_type.__name__ if exc_type else 'None') with CM(): print('Inside') raise ValueError('Oops')
Attempts:
2 left
💡 Hint
The __exit__ method receives exception info if an error happens.
✗ Incorrect
The __enter__ prints 'Start'. The block prints 'Inside' then raises ValueError. The __exit__ runs next, printing 'End' and the exception type 'ValueError'.
❓ Predict Output
advanced2:00remaining
What is the output when __exit__ suppresses the exception?
This context manager suppresses exceptions by returning True from __exit__. What will be printed?
Python
class SuppressError: def __enter__(self): print('Begin') def __exit__(self, exc_type, exc_val, exc_tb): print('Cleanup') return True with SuppressError(): print('Running') raise RuntimeError('Fail') print('After with')
Attempts:
2 left
💡 Hint
Returning True from __exit__ stops the exception from propagating.
✗ Incorrect
The __enter__ prints 'Begin'. The block prints 'Running' then raises RuntimeError. __exit__ runs, prints 'Cleanup', and returns True to suppress the error. So 'After with' prints next.
❓ Predict Output
advanced2:00remaining
What is the output of nested with statements with context managers?
Given these two context managers used in nested with statements, what will be printed?
Python
class CM1: def __enter__(self): print('Enter CM1') def __exit__(self, exc_type, exc_val, exc_tb): print('Exit CM1') class CM2: def __enter__(self): print('Enter CM2') def __exit__(self, exc_type, exc_val, exc_tb): print('Exit CM2') with CM1(): with CM2(): print('Inside both')
Attempts:
2 left
💡 Hint
The inner with runs fully before the outer with exits.
✗ Incorrect
The outer __enter__ runs first printing 'Enter CM1'. Then inner __enter__ prints 'Enter CM2'. The block prints 'Inside both'. Then inner __exit__ prints 'Exit CM2'. Finally outer __exit__ prints 'Exit CM1'.
🧠 Conceptual
expert3:00remaining
What is the value of variable 'result' after this with statement?
Analyze this code. What will be the value of 'result' after the with block finishes?
Python
class CM: def __enter__(self): return 10 def __exit__(self, exc_type, exc_val, exc_tb): return True result = None with CM() as val: result = val + 5 raise Exception('Error') print('Done')
Attempts:
2 left
💡 Hint
Even if an exception is raised, if __exit__ returns True, the block completes and variables keep their values.
✗ Incorrect
The __enter__ returns 10, assigned to val. Inside the block, result = 10 + 5 = 15. The exception is raised but __exit__ returns True, suppressing it. So the program continues and result keeps 15.