0
0
Pythonprogramming~20 mins

Why exceptions occur in Python - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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")
AZeroDivisionError
B0
CCannot divide by zero
D10
Attempts:
2 left
💡 Hint
Think about what happens when you divide a number by zero in Python.
Predict Output
intermediate
2: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])
AIndexError
BTypeError
CKeyError
DValueError
Attempts:
2 left
💡 Hint
Think about what happens when you try to access a list element outside its range.
Predict Output
advanced
2: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')
AKey not found
BNone
CKeyError
D2
Attempts:
2 left
💡 Hint
What happens when you try to access a dictionary key that does not exist?
Predict Output
advanced
2: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)
ASyntaxError
BValueError
CNameError
DTypeError
Attempts:
2 left
💡 Hint
Think about adding a number and a string in Python.
🧠 Conceptual
expert
3: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()
ABecause x is not defined anywhere in the program
BBecause x is assigned inside the function after being used, making it a local variable before assignment
CBecause print cannot be used inside a function
DBecause functions cannot access variables outside their scope
Attempts:
2 left
💡 Hint
Think about how Python treats variables assigned inside functions.