Skip to content

This textbook is in beta – content is actively being refined. Report issues or suggestions

04.07 Common Errors And Exceptions - Quiz

Check your understanding

  1. What type of error prevents Python from running your code at all?

    • Runtime errors
    • Logic errors
    • Syntax errors { data-correct }
    • Type errors
  2. Which error occurs when you try to access list[5] in a list with only 3 elements?

    • IndexError { data-correct }
    • KeyError
    • ValueError
    • TypeError
  3. What causes a ZeroDivisionError?

    • Dividing a number by zero { data-correct }
    • Multiplying by zero
    • Adding zero to a number
    • Subtracting zero from a number
  4. In the condition if age >= 13 or age <= 65:, what’s the logic error?

    • Should use ‘and’ instead of ‘or’ { data-correct }
    • Should use > instead of >=
    • Should use < instead of <=
    • Nothing is wrong with this condition
  5. What’s an off-by-one error?

    • Using the wrong variable name
    • Accessing an index that’s one position off from intended { data-correct }
    • Forgetting to import a module
    • Using the wrong data type
  6. Which would cause a NameError?

    • print(totall) when variable is named ‘total’ { data-correct }
    • print(5 + “hello”)
    • print(my_list[100]) on a short list
    • print(my_dict[“missing_key”])
  7. What makes a loop infinite?

    • The loop condition never becomes False { data-correct }
    • Using a while loop instead of for loop
    • Not using a break statement
    • Having too many iterations
  8. Which is the best way to handle division by zero?

    • Ignore it and hope it doesn’t happen
    • Check if denominator is zero before dividing { data-correct }
    • Use only whole numbers
    • Avoid division operations
  9. What type of error is it when your code runs but calculates the wrong answer?

    • Syntax error
    • Runtime error
    • Logic error { data-correct }
    • Import error
  10. In range(n) where n=5, what values does the range include?

    • 1, 2, 3, 4, 5
    • 0, 1, 2, 3, 4 { data-correct }
    • 0, 1, 2, 3, 4, 5
    • 1, 2, 3, 4
  11. Which is an example of defensive programming?

    • Writing code as fast as possible
    • Validating inputs before using them { data-correct }
    • Using only simple data types
    • Avoiding functions entirely
  12. What should you do when you get an error message?

    • Immediately ask for help
    • Read the message carefully to understand what went wrong { data-correct }
    • Delete the problematic code
    • Restart the program