Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 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!
Options
β€’ NameError
β€’ ValueError
β€’ TypeError
β€’ KeyError

Hint : 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)
Options
β€’ Line c - int("Aquachu")
β€’ Line b - int("25.5")
β€’ Both b and c
β€’ Line a - 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:

Items to match
β€’ print(undefined_var)
β€’ result = 10 / 0
β€’ "text" + 5
β€’ stats = {"hp": 100} stats["attack"]
Definitions
β€’ NameError
β€’ ZeroDivisionError
β€’ TypeError
β€’ KeyError

Exercise 4 β€” Quiz: Dictionary Access Error

What will happen when this code runs?

codemon = {"name": "Aquachu", "level": 15} print(codemon["name"]) print(codemon["type"])
Options
β€’ Prints "Aquachu" then raises KeyError
β€’ Prints "Aquachu" then prints None
β€’ Raises KeyError immediately
β€’ Prints both values successfully

Hint : 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?

Options
β€’ ZeroDivisionError when dividing by zero
β€’ Missing colon after if statement
β€’ Incorrect indentation
β€’ Misspelled keyword like prnit instead of print

Hint : 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")
Options
β€’ The program continues normally and executes the code after the try/except
β€’ The program stops immediately after the except block
β€’ The program skips all remaining code in the file
β€’ The program restarts from the beginning

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")
Options
β€’ "Division successful" and "Program continues"
β€’ Only "Program continues"
β€’ "Cannot divide by zero" and "Program continues"
β€’ Only "Division successful"

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?

Options
β€’ The error type, line number, and a description of what went wrong
β€’ Only the line number where the error occurred
β€’ Only the error type without any details
β€’ A suggestion for how to fix the code

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?

Options
β€’ The program crashes and stops immediately
β€’ The error is ignored and the program continues
β€’ Python automatically fixes the error
β€’ The program asks the user what to do

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}")
Options
β€’ Stores the exception object in variable e so you can access the error message
β€’ Prevents the exception from being raised
β€’ Creates a new error called e
β€’ Makes the exception handling optional

Hint : 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:

Items to match
β€’ Converting user input to integer
β€’ Dividing by a variable that might be zero
β€’ Accessing a dictionary key that might not exist
β€’ Accessing a list index that might be out of range
Definitions
β€’ ValueError
β€’ ZeroDivisionError
β€’ KeyError
β€’ IndexError

Exercise 14 β€” Quiz: Specific vs General Exception Handling

Why is except ValueError: better than just except:?

Options
β€’ It's more precise and helps with debugging by catching only the expected error
β€’ It makes the program run faster
β€’ It catches more types of errors
β€’ It's required by Python syntax

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")
Options
β€’ The ValueError block
β€’ The TypeError block
β€’ The general except: block
β€’ All three blocks

Hint : 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")
Options
β€’ Only ONE - the first matching except block
β€’ All except blocks that match the error type
β€’ Only the most specific except block
β€’ All three except blocks in order

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")
Options
β€’ The general except: catches all errors first, so specific handlers never execute
β€’ You can't have more than two except blocks
β€’ ValueError and TypeError can't be used together
β€’ Nothing is wrong with this order

Hint : 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")
Options
β€’ "Key not found"
β€’ "Invalid value"
β€’ "Unknown error"
β€’ All three messages

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:

Items to match
β€’ int("25.5")
β€’ [1, 2][5]
β€’ "level" + 10
β€’ {"hp": 100}["mp"]
Definitions
β€’ 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?

Options
β€’ Only when NO exception occurs in the try block
β€’ Only when an exception is caught by except
β€’ Always, regardless of exceptions
β€’ Before the try block executes

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?

Options
β€’ It ALWAYS executes, whether an exception occurs or not
β€’ It only executes when an exception occurs
β€’ It only executes when NO exception occurs
β€’ It prevents exceptions from being raised

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")
Options
β€’ A, B, D
β€’ A, C, D
β€’ A, B, C, D
β€’ B, 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.

Back to the course