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

1 comment:

perlknowledge said...

Absolutely. Here is the Python try-except quiz with answers in a single-page format.

Python try-except & Exception Handling — Top 10

1. What is the output?

try:
print(10 / 0)
except ZeroDivisionError:
print("Error")

A) Error
B) 0
C) 10
D) No output

✅ Answer: A) Error


---

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

A) Python ignores the exception
B) The program continues normally
C) The program raises the exception
D) The exception becomes None

✅ Answer: C) The program raises the exception


---

3. True or False:
A finally block normally executes whether or not an exception occurs.

✅ Answer: True


---

4. What is the output?

try:
x = int("abc")
except ValueError:
print("Invalid")
else:
print("Valid")

A) Invalid
B) Valid
C) abc
D) No output

✅ Answer: A) Invalid


---

5. Which can explicitly raise an exception in Python?

A) raise ValueError("Invalid")
B) raise Exception("Problem")
C) throw ValueError("Invalid")
D) raise RuntimeError("Failed")

✅ Answers: A, B, D


---

6. True or False:
The except block must always specify an exception type.

✅ Answer: False
A bare except: is valid Python syntax.


---

7. What is the output?

try:
print(5 / 1)
except ZeroDivisionError:
print("Error")
else:
print("Success")

A) Error
B) 5 then Error
C) 5.0 then Success
D) Success only

✅ Answer: C) 5.0 then Success


---

8. Which exception occurs when accessing a dictionary key that does not exist?

A) IndexError
B) NameError
C) TypeError
D) KeyError

✅ Answer: D) KeyError


---

9. What does the else block in try-except-else do?

A) Executes only when an exception occurs
B) Executes after try completes successfully
C) Always executes before try
D) Replaces finally

✅ Answer: B) Executes after try completes successfully


---

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) Done only
C) Index problem only
D) 5 then Done

✅ Answer: A) Index problem then Done

Answer Key

1-A | 2-C | 3-True | 4-A | 5-A,B,D | 6-False | 7-C | 8-D | 9-B | 10-A