04.07 Common Errors And Exceptions - Quiz¶
Check your understanding
-
What type of error prevents Python from running your code at all?
- Runtime errors
- Logic errors
- Syntax errors { data-correct }
- Type errors
-
Which error occurs when you try to access list[5] in a list with only 3 elements?
- IndexError { data-correct }
- KeyError
- ValueError
- TypeError
-
What causes a ZeroDivisionError?
- Dividing a number by zero { data-correct }
- Multiplying by zero
- Adding zero to a number
- Subtracting zero from a number
-
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
-
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
-
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”])
-
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
-
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
-
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
-
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
-
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
-
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