Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Which Error Type?
What error occurs when you try to use a variable that was never created?
print(codemon_power)
# codemon_power was never defined!NameErrorValueErrorTypeErrorKeyErrorHint : This error happens when Python doesn't recognize a variable name because it was never created.
Exercise 2 β Quiz: Type Conversion Error
Which line will cause a ValueError?
a = int("100")
b = int("25.5")
c = int("Aquachu")
d = str(100)c - int("Aquachu")b - int("25.5")b and ca - int("100")Hint : int() can only convert strings that contain whole numbers. Think about which strings are not valid integers.
Exercise 3 β Quiz: Match Code to Error Type
Match each code snippet with the error it will produce:
print(undefined_var)result = 10 / 0"text" + 5stats = {"hp": 100}
stats["attack"]NameErrorZeroDivisionErrorTypeErrorKeyErrorExercise 4 β Quiz: Dictionary Access Error
What will happen when this code runs?
codemon = {"name": "Aquachu", "level": 15}
print(codemon["name"])
print(codemon["type"])KeyErrorNoneKeyError immediatelyHint : The first print works because "name" exists. What happens when we try to access a key that doesn't exist?
Exercise 5 β Quiz: Syntax vs Runtime Error
Which of these is a runtime error, not a syntax error?
ZeroDivisionError when dividing by zeroif statementprnit instead of printHint : Runtime errors occur during program execution. Syntax errors are detected before the code runs.
Exercise 6 β Quiz: Exception Recovery
What happens to code AFTER a try/except block if an exception was caught?
try:
result = 10 / 0
except ZeroDivisionError:
print("Error handled")
result = 0
print(f"Result: {result}")
print("Program continues")Hint : Think about the purpose of exception handling - it's meant to prevent crashes and allow the program to continue.
Exercise 7 β Quiz: Exception Handling Flow
What will be printed when this code runs?
try:
result = 10 / 2
print("Division successful")
except ZeroDivisionError:
print("Cannot divide by zero")
print("Program continues")Hint : The division is valid (10/2), so no exception occurs. What executes when there's no error?
Exercise 8 β Quiz: Understanding Error Messages
What information does a Python error message typically include?
Hint : Think about what you see in the terminal when an error occurs - there's usually multiple pieces of useful information.
Exercise 9 β Quiz: Program Behavior Without Exception Handling
What happens if an exception occurs and there's NO try/except to handle it?
Hint : Think about what you've seen when running code with errors before learning about exception handling.
Exercise 10 β Quiz: Handle Division Error
Complete the code to safely calculate damage, using 0 if division fails:
Hint : Use the basic try/except structure to catch the division by zero error.
Exercise 11 β Quiz: Catch Specific ValueError
Complete the code to catch the exception that will happen when converting a non-numeric string to an integer:
Hint : What specific error occurs when converting an invalid string to int?
Exercise 12 β Quiz: Exception Message Access
What does the as e syntax do in exception handling?
try:
result = int("abc")
except ValueError as e:
print(f"Error: {e}")e so you can access the error messageeHint : Think about why you might want to give the exception a variable name.
Exercise 13 β Quiz: Match Exception to Situation
Match each situation with the specific exception you should catch:
ValueErrorZeroDivisionErrorKeyErrorIndexErrorExercise 14 β Quiz: Specific vs General Exception Handling
Why is except ValueError: better than just except:?
Hint : Think about what happens if an unexpected error occurs with a general except.
Exercise 15 β Quiz: Use Exception Message
Complete the code to catch the exception and display its error message:
Hint : Use the syntax that allows you to access the exception object and its message.
Exercise 16 β Quiz: Multiple Exception Handlers
Which except block will execute if int("abc") is called?
try:
level = int("abc")
except ValueError:
print("Invalid number")
except TypeError:
print("Wrong type")
except:
print("Other error")ValueError blockTypeError blockexcept: blockHint : int() with an invalid string raises ValueError. Python executes only the first matching except block.
Exercise 17 β Quiz: Exception Handling Priority
How many except blocks will execute if an exception occurs?
try:
codemon_hp = int("invalid")
except ValueError:
print("Invalid value")
except TypeError:
print("Wrong type")
except Exception:
print("General error")Hint : Python stops checking except blocks as soon as it finds one that matches the exception type.
Exercise 18 β Quiz: Exception Handler Order Matters
What's wrong with this exception handling order?
try:
result = risky_operation()
except:
print("General error")
except ValueError:
print("Value error")
except TypeError:
print("Type error")except: catches all errors first, so specific handlers never executeHint : Python checks except blocks from top to bottom. What happens when a general except comes first?
Exercise 19 β Quiz: Which Handler Executes?
What will be printed if the dictionary access fails?
codemon = {"name": "Aquachu"}
try:
print(codemon["level"])
except KeyError:
print("Key not found")
except ValueError:
print("Invalid value")
except:
print("Unknown error")Hint : Accessing a non-existent dictionary key raises KeyError. Only one except block executes.
Exercise 20 β Quiz: Match Code to Exception Handler
Match each code operation with the except block that would handle its error:
int("25.5")[1, 2][5]"level" + 10{"hp": 100}["mp"]except ValueError:except IndexError:except TypeError:except KeyError:Exercise 21 β Quiz: When else Executes
When does the else clause execute in a try/except/else block?
Hint : Think about what makes else special - it's for the success case.
Exercise 22 β Quiz: finally Clause Behavior
What is TRUE about the finally clause?
Hint : finally is used for cleanup code that must run no matter what happens.
Exercise 23 β Quiz: Complete try/except/else/finally
Complete the full exception handling structure:
Hint : Order: risky code, error handling, success code, cleanup code.
Exercise 24 β Quiz: Execution Order with Exception
What is the execution order when an exception OCCURS?
try:
print("A")
x = 1 / 0
except ZeroDivisionError:
print("B")
else:
print("C")
finally:
print("D")Hint : When exception occurs: try runs until error, then except, else is skipped, finally always runs.
Exercise 25 β Quiz: Practical else and finally Usage
Complete the code: use else for success message and finally for cleanup:
Hint : else runs only on success (no exception). finally always runs for cleanup.