Search Tutorials


Python Exception Handling MCQ Questions and Answers | JavaInUse

Python Exception Handling MCQ Questions and Answers

Q. What is the purpose of exception handling in Python?

A. To handle and recover from errors or exceptional conditions during program execution
B. To generate errors and interrupt the normal flow of the program
C. To provide information about the program's progress
D. To optimize the performance of the code

Q. What is the keyword used to raise an exception manually in Python?

A. throw
B. raise
C. generate
D. create

Q. What is the purpose of the "try" block in exception handling?

A. To specify the code that may raise an exception
B. To handle and recover from exceptions
C. To define custom exception classes
D. To specify the code that should be executed regardless of whether an exception occurs

Q. What is the correct order of execution when an exception is raised inside a "try" block?

A. try block -> except block -> finally block
B. try block -> finally block -> except block
C. except block -> try block -> finally block
D. finally block -> try block -> except block

Q. What is the purpose of the "except" block in exception handling?

A. To specify the code that raises an exception
B. To handle and deal with exceptions that occur in the "try" block
C. To define the exception classes to be caught
D. To specify the code that should be executed if no exception occurs

Q. What is the purpose of the "finally" block in exception handling?

A. To specify the code that raises an exception
B. To handle and deal with exceptions
C. To define the exception classes
D. To specify the code that should be executed regardless of whether an exception occurs

Q. What is the purpose of the "raise" statement inside an exception class?

A. To create a new exception class
B. To manually raise an exception of that class
C. To specify the error message for the exception
D. To define the actions to be taken when the exception is caught

Q. How can you specify multiple exception classes to be caught in a single "except" block?

A. By separating the class names with commas
B. By using a list of class names
C. By using a tuple of class names
D. By using a dictionary with class names as keys

Q. What is the purpose of the "as" keyword in an "except" block?

A. To assign a name to the exception object
B. To specify the exception class
C. To provide additional information about the exception
D. To define a custom error message

Q. What is the purpose of the "else" block in exception handling?

A. To specify the code that should be executed if no exception occurs
B. To handle and deal with exceptions
C. To define custom exception classes
D. To specify the code that raises an exception

Q. What is the purpose of the "with" statement in exception handling?

A. To handle exceptions that occur within a context
B. To define custom exception classes
C. To specify the code that should be executed regardless of whether an exception occurs
D. To raise an exception manually

Q. What is the purpose of the "assert" statement in Python?

A. To raise an AssertionError if a condition is false
B. To handle and recover from exceptions
C. To define custom exception classes
D. To specify the code that should be executed if a condition is true

Q. What is the purpose of the "try-except-else" construct?

A. To handle exceptions and provide an alternative code path if no exception occurs
B. To define custom exception classes and their handling
C. To specify the code that should be executed before and after exception handling
D. To raise an exception and provide additional information about it

Q. What is the purpose of the "try-finally" construct?

A. To handle exceptions and ensure that cleanup code is executed
B. To define custom exception classes and their handling
C. To specify the code that should be executed before and after exception handling
D. To raise an exception and provide additional context

Q. What is the purpose of the "try-except-finally" construct?

A. To handle exceptions, provide alternative code paths, and ensure cleanup
B. To define custom exception classes and their handling
C. To specify the code that should be executed before and after exception handling
D. To raise an exception and provide detailed information about it

Q. What is the purpose of exception handling in Python?

A. To handle errors and prevent program termination
B. To provide alternative code paths for different exceptions
C. To generate custom error messages for users
D. All of the above

Q. What is the correct order of exception handling blocks in Python?

A. try - except - finally
B. try - finally - except
C. except - try - finally
D. finally - try - except

Q. What is the purpose of the "try" block in exception handling?

A. To specify the code that might raise an exception
B. To handle the exception and provide alternative code
C. To display an error message to the user
D. To specify the code that will always execute

Q. What is the purpose of the "except" block in exception handling?

A. To specify the type of exception to catch
B. To provide alternative code to execute when an exception occurs
C. Both A and B
D. To re-raise the exception to a higher level

Q. What is the purpose of the "finally" block in exception handling?

A. To specify code that always executes, regardless of exceptions
B. To provide a way to re-raise an exception
C. To log the details of the exception
D. To specify the type of exception to catch

Q. What is the difference between "try-except" and "try-except-else" blocks?

A. "try-except-else" allows you to specify alternative code that executes if no exception occurs.
B. "try-except" can catch multiple exceptions, while "try-except-else" can only catch a single exception.
C. "try-except-else" is more efficient than "try-except" in terms of performance.
D. "try-except-else" is used for raising exceptions, while "try-except" is used for catching them.

Q. How can you catch multiple exceptions in a single "except" block?

A. By separating the exception classes with a comma
B. By using a list of exception classes
C. By using a tuple of exception classes
D. By providing multiple "except" blocks, each catching a single exception

Q. What is the purpose of the "raise" keyword in exception handling?

A. To manually raise an exception
B. To specify the type of exception to catch
C. To re-raise an exception to a higher level
D. To provide alternative code to execute when an exception occurs

Q. What is the purpose of the "assert" statement in Python?

A. To verify that a condition is true, and raise an AssertionError if it is false
B. To catch and handle exceptions
C. To specify alternative code to execute when an exception occurs
D. To log error messages

Q. What is the difference between "try-except" and "try-else" blocks?

A. "try-except" catches exceptions, while "try-else" specifies alternative code.
B. "try-except" can catch multiple exceptions, while "try-else" can only catch a single exception.
C. "try-else" is more efficient than "try-except" in terms of performance.
D. "try-else" is used for raising exceptions, while "try-except" is used for handling them.

Q. How can you create a custom exception class in Python?

A. By subclassing the built-in Exception class
B. By creating a new class that raises an exception
C. By using the "raise" keyword and providing a custom error message
D. By importing and using a third-party exception handling library

Q. What is the purpose of the "with" statement in exception handling?

A. To provide a shorthand for common exception handling scenarios
B. To ensure proper resource cleanup, regardless of exceptions
C. To catch and handle exceptions raised within the block
D. To specify code that should be executed only if an exception occurs

Q. What is the purpose of the "as" keyword in an exception handling block?

A. To assign a variable name to the exception object
B. To specify additional information about the exception
C. To provide a custom error message for the exception
D. To specify the type of exception to catch

Q. What is the purpose of the "traceback" module in Python?

A. To provide detailed information about exceptions and their stack traces
B. To create custom exception classes
C. To handle exceptions and provide alternative code paths
D. To log error messages to a file

Q. How can you access the details of the last exception raised in Python?

A. By using the "last_exception()" function
B. By accessing the "__exception__" global variable
C. By using the "get_last_exception()" function from the "exceptions" module
D. By accessing the "last_exception" attribute of the "sys" module