Exception handling is a critical concept in programming, allowing developers to gracefully handle errors and prevent a complete crash of their applications. Python, with its succinct and readable syntax, provides an intuitive way to manage exceptions using try, except, else, and finally blocks.
In this MCQ page, we curated list of 15+ multiple choice questions on exception handling in Python for beginners. Are you ready to test your knowledge? Let’s jump in!
Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. What does the try block do in Python exception handling?
Answer:
Explanation:
The try block contains the code segment that might produce an exception. If an exception occurs, the code inside the try block stops executing, and control passes to the except block.
2. Which keyword is used to handle exceptions in Python?
Answer:
Explanation:
The except block contains the code to handle or catch the exception that was raised in the try block.
3. Which of the following is not a built-in exception in Python?
Answer:
Explanation:
Python doesn’t have a FileNotFoundException exception. Instead, it has FileNotFoundError.
4. If multiple except blocks are available, how does Python choose the right one?
Answer:
Explanation:
Python goes through each except block sequentially and stops at the first matching block.
5. What is the purpose of the else block in exception handling?
Answer:
Explanation:
The else block executes if no exception was raised in the try block.
6. Which block is always executed irrespective of whether an exception was raised or not?
Answer:
Explanation:
The finally block always executes, making it useful for cleanup tasks like closing a file or a network connection.
7. How can you raise a custom exception in Python?
Answer:
Explanation:
The raise keyword is used to raise exceptions in Python.
8. Which exception is raised when you try to use a variable that hasn’t been defined?
Answer:
Explanation:
A NameError is raised when a local or global name is not found.
9. What will the output of the following code be?
try:
x = 1/0
except ZeroDivisionError:
x = 0
print(x)
Answer:
Explanation:
The ZeroDivisionError exception is caught and handled by setting x to 0.
10. Which exception is thrown when you try to import a module that doesn’t exist?
Answer:
Explanation:
An ImportError is raised when the import statement can’t find the specified module.
11. If no exception type is mentioned in the except block, which kind of exception will it catch?
Answer:
Explanation:
An except block without a specified exception type will catch all exceptions.
12. Which of the following is the base class for all built-in exceptions?
Answer:
Explanation:
BaseException is the base class from which all built-in exceptions are derived. However, for user-defined exceptions, it’s recommended to derive from the Exception class.
13. Which exception is raised when a program runs out of memory?
Answer:
Explanation:
A MemoryError is raised when an operation runs out of memory.
14. If you have a try block nested inside another try block and an exception occurs in the inner try block that is not handled there, where will Python look for the next matching except block?
Answer:
Explanation:
If an inner try block does not handle an exception, Python will look for an except block in the immediate enclosing try block.
15. What does the as keyword do in the context of exception handling?
Answer:
Explanation:
The as keyword is used to create an alias for the exception, allowing you to reference it in your except block.