Challenge - 5 Problems
Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code with division by zero?
Consider the following Python code. What will it output when run?
Python
try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero")
Attempts:
2 left
💡 Hint
Think about what happens when you divide a number by zero in Python.
✗ Incorrect
Dividing by zero causes a ZeroDivisionError. The except block catches this and prints the message.
❓ Predict Output
intermediate2:00remaining
What error does this code raise?
What error will this code produce when run?
Python
my_list = [1, 2, 3] print(my_list[5])
Attempts:
2 left
💡 Hint
Think about what happens when you try to access a list element outside its range.
✗ Incorrect
Accessing an index that does not exist in a list raises an IndexError.
❓ Predict Output
advanced2:00remaining
What is the output of this code with a missing key in a dictionary?
What will this code print when executed?
Python
my_dict = {'a': 1, 'b': 2}
try:
print(my_dict['c'])
except KeyError:
print('Key not found')Attempts:
2 left
💡 Hint
What happens when you try to access a dictionary key that does not exist?
✗ Incorrect
Accessing a missing key raises KeyError, which is caught and prints 'Key not found'.
❓ Predict Output
advanced2:00remaining
What error does this code raise with wrong type operation?
What error will this code produce when run?
Python
number = 5 text = 'hello' print(number + text)
Attempts:
2 left
💡 Hint
Think about adding a number and a string in Python.
✗ Incorrect
You cannot add an integer and a string directly, which causes a TypeError.
🧠 Conceptual
expert3:00remaining
Why does this code raise an UnboundLocalError?
What is the cause of the error in this code snippet?
Python
x = 10 def func(): print(x) x = 5 func()
Attempts:
2 left
💡 Hint
Think about how Python treats variables assigned inside functions.
✗ Incorrect
Assigning to x inside the function makes x local. Using it before assignment causes UnboundLocalError.