Tuesday, October 6, 2026

Python try-except Quiz

 Python try-except Quiz


1. What is the output? try: print(10 / 0) except ZeroDivisionError: print("Error")

   A. Error

   B. 10

   C. No output

   D. 0


2. What happens if an exception occurs inside try and there is no matching except block?

   A. The finally block is skipped automatically

   B. The exception becomes None

   C. The program raises the exception

   D. Python ignores the exception


3. A finally block normally executes whether or not an exception occurs in the associated try block.

   True

   False


4. What is the output? try: x = int("abc") except ValueError: print("Invalid") else: print("Valid")

   A. No output

   B. Valid

   C. Invalid

   D. abc


5. Which of the following can be used to explicitly raise an exception in Python? Select all that apply.

   A. throw ValueError("Invalid")

   B. raise RuntimeError("Failed")

   C. raise ValueError("Invalid")

   D. raise Exception("Problem")


6. The except block must always specify an exception type.

   True

   False


7. What is the output? try: print(5 / 1) except ZeroDivisionError: print("Error") else: print("Success")

   A. Success only

   B. 5 then Error

   C. 5.0 then Success

   D. Error


8. Which exception is most appropriate when accessing a dictionary key that does not exist?

   A. TypeError

   B. NameError

   C. IndexError

   D. KeyError


9. What does the else block in try-except-else execute when the try block completes successfully?

   A. It executes only when an exception occurs

   B. It executes after the try block

   C. It always executes before try

   D. It replaces finally


10. What is the output? try: numbers = [1, 2, 3] print(numbers[5]) except IndexError: print("Index problem") finally: print("Done")

   A. Index problem then Done

   B. 5 then Done

   C. Index problem only

   D. Done only