Python Exception Handling MCQs | 25 Practice Questions with Answers | CBSE Class 12 Computer Science
Class 12 · Computer Science
Python Exception Handling MCQs
Practice these multiple-choice questions to strengthen your understanding of Python Exception Handling. Click Show Answer to reveal the correct answer and explanation.
Q1. What is an exception in Python?
A. A loop
B. A runtime error that interrupts the normal flow of a program
C. A variable
D. A function
Show Answer
Answer: B. A runtime error that interrupts the normal flow of a program
Explanation: An exception is an error that occurs during program execution and disrupts the normal flow of the program.
Q2. Which keyword is used to write code that may generate an exception?
A. catch
B. try
C. except
D. finally
Show Answer
Answer: B. try
Explanation: The try block contains statements that may generate an exception.
Q3. Which block is executed when an exception occurs?
A. finally
B. else
C. except
D. def
Show Answer
Answer: C. except
Explanation: The except block handles the exception generated in the try block.
Q4. Which block is always executed whether an exception occurs or not?
A. except
B. finally
C. try
D. else
Show Answer
Answer: B. finally
Explanation: The finally block always executes, regardless of whether an exception occurs.
Q5. Which of the following is a valid exception handling structure?
A. try - except
B. try - except - finally
C. try - finally
D. All of these
Show Answer
Answer: D. All of these
Explanation: Python supports try-except, try-finally, and try-except-finally structures.
Q6. What will be the output of the following code?
try:
print(10/2)
except:
print("Error")
A. Error
B. 5.0
C. 5
D. No Output
Show Answer
Answer: B. 5.0
Explanation: Since no exception occurs, the except block is skipped.
Q7. What will be the output of the following code?
try:
print(10/0)
except:
print("Division by Zero")
A. 0
B. Error
C. Division by Zero
D. Infinity
Show Answer
Answer: C. Division by Zero
Explanation: Division by zero raises an exception, so the except block is executed.
Q8. Which exception is generated when a number is divided by zero?
A. ValueError
B. NameError
C. ZeroDivisionError
D. TypeError
Show Answer
Answer: C. ZeroDivisionError
Explanation: Dividing a number by zero raises a ZeroDivisionError.
Q9. Which exception is generated when an undefined variable is used?
A. ValueError
B. NameError
C. IndexError
D. KeyError
Show Answer
Answer: B. NameError
Explanation: A NameError occurs when an undefined variable is referenced.
Q10. What is the main purpose of exception handling?
A. To increase program size
B. To stop program execution permanently
C. To handle runtime errors gracefully without crashing the program
D. To improve program speed
Show Answer
Answer: C. To handle runtime errors gracefully without crashing the program
Explanation: Exception handling allows the program to continue or terminate gracefully after handling runtime errors.
Q11. What will be the output of the following code?
try:
print(15/3)
except:
print("Error")
finally:
print("Done")
A. 5.0
B. Done
C. 5.0
Done
D. Error
Show Answer
Answer: C.
Explanation: The try block executes successfully, and the finally block always executes.
Q12. What will be the output of the following code?
try:
print(10/0)
except:
print("Exception")
finally:
print("Program End")
A. Exception
Program End
B. Program End
C. Exception
D. Error
Show Answer
Answer: A.
Explanation: The exception is handled by the except block, and the finally block executes afterward.
Q13. Which block is optional in exception handling?
A. try
B. except
C. finally
D. Both except and finally
Show Answer
Answer: D. Both except and finally
Explanation: A try block must be followed by at least one except or a finally block. Both except and finally are individually optional.
Q14. Which keyword is used to handle an exception raised inside the try block?
A. catch
B. except
C. finally
D. raise
Show Answer
Answer: B. except
Explanation: The except block catches and handles exceptions raised in the try block.
Q15. Which exception is generated when an invalid list index is accessed?
A. KeyError
B. NameError
C. IndexError
D. ValueError
Show Answer
Answer: C. IndexError
Explanation: Accessing a list element using an invalid index raises an IndexError.
Q16. Which exception is generated when an invalid key is accessed in a dictionary?
A. ValueError
B. IndexError
C. KeyError
D. NameError
Show Answer
Answer: C. KeyError
Explanation: A KeyError occurs when a dictionary key does not exist.
Q17. Which exception is generated when an operation is performed on incompatible data types?
A. NameError
B. TypeError
C. ValueError
D. IndexError
Show Answer
Answer: B. TypeError
Explanation: A TypeError occurs when an operation is performed on incompatible data types.
Q18. Which exception is generated when an invalid value is passed to a function?
A. ValueError
B. NameError
C. ZeroDivisionError
D. KeyError
Show Answer
Answer: A. ValueError
Explanation: A ValueError occurs when the correct data type is used but the value is inappropriate.
Q19. Which statement is true about the finally block?
A. It executes only when an exception occurs.
B. It executes only when no exception occurs.
C. It always executes.
D. It executes only after the except block.
Show Answer
Answer: C. It always executes.
Explanation: The finally block executes regardless of whether an exception occurs.
Q20. Which statement is generally placed inside the try block?
A. Statements that may generate an exception
B. Variable declarations only
C. Comments only
D. Function definitions only
Show Answer
Answer: A. Statements that may generate an exception
Explanation: The try block contains statements that may raise an exception during execution.
Q21. What will be the output of the following code?
try:
print("Python")
except:
print("Error")
finally:
print("Completed")
A. Python
B. Completed
C. Python
Completed
D. Error
Show Answer
Answer: C.
Explanation: The try block executes successfully, and the finally block always executes.
Q22. What will be the output of the following code?
try:
x=5/0
except:
print("Exception Handled")
print("Program Continues")
A. Exception Handled
Program Continues
B. Program Continues
C. Error
D. No Output
Show Answer
Answer: A.
Explanation: The exception is handled by the except block, and the program continues executing the remaining statements.
Q23. Which of the following is the correct order of exception handling blocks?
A. except → try → finally
B. try → except → finally
C. finally → try → except
D. try → finally → except
Show Answer
Answer: B. try → except → finally
Explanation: In Python, the correct order is try, followed by one or more except blocks, and optionally a finally block.
Q24. Which block is mainly used to perform cleanup operations such as closing files or releasing resources?
A. try
B. except
C. finally
D. else
Show Answer
Answer: C. finally
Explanation: The finally block is commonly used for cleanup tasks because it executes whether an exception occurs or not.
Q25. What is the primary advantage of using exception handling in Python?
A. It increases the execution speed of the program.
B. It reduces the size of the program.
C. It allows the program to handle runtime errors gracefully and continue execution whenever possible.
D. It automatically removes all errors from the program.
Show Answer
Answer: C.
Explanation: Exception handling helps prevent abrupt program termination by handling runtime errors in a controlled manner.
Answer Key
| Q.No. | Answer | Q.No. | Answer | Q.No. | Answer |
|---|---|---|---|---|---|
| 1 | B | 10 | C | 19 | C |
| 2 | B | 11 | C | 20 | A |
| 3 | C | 12 | A | 21 | C |
| 4 | B | 13 | D | 22 | A |
| 5 | D | 14 | B | 23 | B |
| 6 | B | 15 | C | 24 | C |
| 7 | C | 16 | C | 25 | C |
| 8 | C | 17 | B | ||
| 9 | B | 18 | A |
Practice Tips
- Understand the purpose of the
try,except, andfinallyblocks. - Remember that the
finallyblock always executes, regardless of whether an exception occurs. - Practice identifying common exceptions such as
ZeroDivisionError,NameError,TypeError,ValueError,IndexError, andKeyError. - Learn the correct sequence of exception handling blocks:
try → except → finally. - Practice output-based questions involving exception handling, as they are frequently asked in CBSE examinations.
- Remember that exception handling makes programs more reliable by preventing unexpected termination due to runtime errors.
- Attempt the MCQs without viewing the answers first, then use the explanations to strengthen your understanding.