Python Exception Handling MCQ Questions and Answers

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 blog post, 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?

a) Executes the code that might raise an exception
b) Catches and handles exceptions
c) Defines the code to be executed after the try block
d) Specifies the final action regardless of exceptions

Answer:

a) Executes the code that might raise an exception

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?

a) catch
b) throw
c) finally
d) except

Answer:

d) except

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?

a) ZeroDivisionError
b) FileNotFoundException
c) ImportError
d) ValueError

Answer:

b) FileNotFoundException

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?

a) Randomly
b) First except block that matches
c) Last except block that matches
d) Executes all matching blocks

Answer:

b) First except block that matches

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?

a) Handles exceptions
b) Executes if no exception occurs
c) Executes if an exception occurs
d) Always executes after the try block

Answer:

b) Executes if no exception occurs

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?

a) try
b) except
c) else
d) finally

Answer:

d) finally

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?

a) throw Exception("Message")
b) raise Exception("Message")
c) catch Exception("Message")
d) generate Exception("Message")

Answer:

b) raise Exception("Message")

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?

a) UndefinedVariableError
b) ReferenceError
c) ValueError
d) NameError

Answer:

d) NameError

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)
a) 1
b) 0
c) An exception will be raised
d) None

Answer:

b) 0

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?

a) ImportError
b) ModuleError
c) FileNotFoundError
d) ValueError

Answer:

a) ImportError

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?

a) No exceptions
b) Only built-in exceptions
c) Only user-defined exceptions
d) All exceptions

Answer:

d) All exceptions

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?

a) BaseError
b) Error
c) BaseException
d) Exception

Answer:

c) BaseException

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?

a) OutOfMemoryError
b) MemoryError
c) MemoryOverflowError
d) RuntimeError

Answer:

b) MemoryError

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?

a) In the immediate outer try block
b) In the main program outside of all try blocks
c) In the global scope
d) It will terminate immediately

Answer:

a) In the immediate outer try block

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?

a) Renames the exception
b) Provides an alias for the exception
c) Ignores the exception
d) Re-raises the exception

Answer:

b) Provides an alias for the exception

Explanation:

The as keyword is used to create an alias for the exception, allowing you to reference it in your except block.



Comments