Chaining & Context Managers

Preview — 3 of 10 questions

What is the output of the following code?

javascript
try:
    try:
        1 / 0
    except ZeroDivisionError as e:
        raise ValueError("invalid calculation") from e
except ValueError as e:
    print(e)
    print(e.__cause__)
Ainvalid calculation then division by zero
Binvalid calculation then None
CTypeError: raise ... from ... requires two exception instances
Ddivision by zero then invalid calculation

What is the output of the following code?

javascript
try:
    try:
        1 / 0
    except ZeroDivisionError:
        raise ValueError("invalid calculation")
except ValueError as e:
    print(e.__cause__)
    print(e.__context__)
ANone then None
Bdivision by zero then division by zero
CNone then division by zero
Ddivision by zero then None

What is the output of the following code?

javascript
from contextlib import contextmanager

@contextmanager
def timer_context():
    print("start")
    yield "timer object"
    print("end")

with timer_context() as t:
    print(f"using {t}")
Ausing timer object, start, end
Bstart, using timer object, end
Cstart, end, using timer object
DTypeError: contextmanager() missing required argument

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.