12 Class- Exception Handling in Python objective

Q1. What is the purpose of using Exception Handling in Python?

a) To prevent the occurrence of errors in the program
b) To ignore the errors that occur in the program
c) To identify and handle errors that occur in the program
d) To stop the execution of the program when errors occur

Answer: c) To identify and handle errors that occur in the program

Explanation: Exception handling is used to identify and handle errors that occur during the execution of a program. By using exception handling, we can gracefully handle errors and prevent the program from crashing.

Q2. What is an Exception in Python?

a) An error that occurs during the execution of a program
b) A warning message that is displayed during the execution of a program
c) A syntax error that occurs when writing code in Python
d) A logical error that occurs when the program does not produce the expected output

Answer: a) An error that occurs during the execution of a program

Explanation: An exception is an error that occurs during the execution of a program. It is caused by something unexpected happening during the execution of a program, such as a divide by zero error, a file not found error, or a type error.

Q3. What is the syntax for handling exceptions in Python?

a) try-except
b) try-catch
c) try-exception
d) try-finally

Answer: a) try-except

Explanation: The syntax for handling exceptions in Python is try-except. The try block contains the code that might raise an exception, and the except block contains the code that will handle the exception if it occurs.

Q4. Which of the following is NOT a type of built-in exception in Python?

a) ZeroDivisionError
b) TypeError
c) FileNotFoundError
d) DatabaseError

Answer: d) DatabaseError

Explanation: While ZeroDivisionError, TypeError, and FileNotFoundError are all built-in exceptions in Python, DatabaseError is not. DatabaseError is a base class for exceptions that occur when interacting with a database, but it is not a built-in exception in Python.

Q5. What is the purpose of the else block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the finally block has executed
d) To ignore the exception that occurred in the try block

Answer: b) To execute code if no exception occurred in the try block

Explanation: The else block in Python exception handling is used to execute code if no exception occurred in the try block. If an exception occurs in the try block, the code in the else block will not be executed. The else block is optional and can be omitted if it is not needed.

Q6. What is the purpose of the finally block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not
d) To ignore the exception that occurred in the try block

Answer: c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not

Explanation: The finally block in Python exception handling is used to execute code after the try and except blocks have executed, regardless of whether an exception occurred or not. This is useful for cleaning up resources, closing files, and releasing locks. The finally block is optional and can be omitted if it is not needed.

Q7. Can multiple except blocks be used in a single try block in Python?

a) No, only one except block can be used in a try block
b) Yes, multiple except blocks can be used in a try block to handle different types of exceptions
c) Yes, but each except block must be enclosed in its own try block
d) No, each exception must be handled separately in a separate function

Answer: b) Yes, multiple except blocks can be used in a try block to handle different types of exceptions

Explanation: Multiple except blocks can be used in a single try block in Python to handle different types of exceptions. Each except block will catch a specific type of exception, and the code in that block will be executed if that type of exception occurs.

Q8. What is the purpose of the raise statement in Python?

a) To raise an exception manually
b) To handle an exception that occurred in the program
c) To ignore an exception that occurred in the program
d) To exit the program when an exception occurs

Answer: a) To raise an exception manually

Explanation: The raise statement in Python is used to raise an exception manually. This is useful when you want to handle a specific error condition in your program and raise an exception to signal that condition. When a raise statement is executed, it causes the specified exception to be raised, which can then be handled by an except block.

Q9. What is the difference between a try-except block and a try-finally block in Python?

a) A try-except block is used to handle exceptions, while a try-finally block is used for cleanup code
b) A try-except block is used for cleanup code, while a try-finally block is used to handle exceptions
c) A try-except block is used to handle exceptions, while a try-finally block is used to execute code after the try and except blocks have executed
d) There is no difference between a try-except block and a try-finally block in Python

Answer: c) A try-except block is used to handle exceptions, while a try-finally block is used to execute code after the try and except blocks have executed

Explanation: The try-except block in Python is used to handle exceptions that may occur during the execution of a program, while the try-finally block is used to execute code after the try and except blocks have executed, regardless of whether an exception occurred or not. The try-finally block is useful for cleanup code, such as releasing resources or closing files, that needs to be executed regardless of whether an exception occurred.

 

Q10. What is the recommended approach to handling exceptions in Python?

a) Ignoring exceptions and letting the program crash if an error occurs
b) Using try-except blocks to handle exceptions as they occur
c) Using try-finally blocks to handle exceptions as they occur
d) Using if statements to check for potential

Q11. What is the syntax for raising a custom exception in Python?

a) raise Exception(“Custom Exception”)
b) raise custom_exception(“Custom Exception”)
c) raise custom_exception
d) custom_exception(“Custom Exception”)

Answer: b) raise custom_exception(“Custom Exception”)

Explanation: To raise a custom exception in Python, you need to define a new exception class that inherits from the built-in Exception class. You can then raise an instance of your custom exception class using the raise statement, passing a message string as an argument. The syntax for raising a custom exception is: raise custom_exception(“Custom Exception”).

Q12. What is the purpose of the else block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not
d) To ignore the exception that occurred in the try block

Answer: b) To execute code if no exception occurred in the try block

Explanation: The else block in Python exception handling is used to execute code if no exception occurred in the try block. This block is optional and follows all the except blocks. If no exception occurred in the try block, the code in the else block will be executed. If an exception occurred, the code in the else block will be skipped.

Q13. What is the purpose of the assert statement in Python?

a) To raise an exception if a condition is not true
b) To handle an exception that occurred in the program
c) To ignore an exception that occurred in the program
d) To exit the program when an exception occurs

Answer: a) To raise an exception if a condition is not true

Explanation: The assert statement in Python is used to raise an exception if a condition is not true. The syntax of the assert statement is: assert condition, message. If the condition is true, the program continues to execute normally. If the condition is false, an AssertionError is raised with the specified error message.

Q14. What is the purpose of the traceback module in Python?

a) To handle exceptions that occur in the program
b) To display a traceback of the current call stack
c) To ignore exceptions that occur in the program
d) To exit the program when an exception occurs

Answer: b) To display a traceback of the current call stack

Explanation: The traceback module in Python is used to display a traceback of the current call stack. A traceback is a report of the function calls that led up to an exception. It shows the line of code that caused the exception and the line of code that called that line, and so on, all the way back to the top-level of the program. The traceback module can be used to display this information in a human-readable format.

Q15. What is the purpose of the finally block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not
d) To ignore the exception that occurred in the try block

Answer: c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not

Explanation: The finally block in Python exception handling is used to execute code after the try and except blocks have executed, regardless of whether an exception occurred or not. This block is optional and follows all the except blocks. The code in the finally block is executed even if an exception occurred in the try block and was handled by an except block, or if the try block executed successfully with no exceptions. The purpose of the finally block is to ensure that certain cleanup actions, such as closing a file or a network connection, are always performed.

Q16. Which of the following is an example of a built-in exception in Python?

a) FileExistsError
b) FileNotFoundError
c) ZeroDivisionError
d) TypeError

Answer: c) ZeroDivisionError

Explanation: ZeroDivisionError is an example of a built-in exception in Python. It is raised when attempting to divide by zero. The other options are also examples of built-in exceptions in Python, but they are related to file operations and data types, respectively.

Q17. What is the purpose of the except block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not
d) To ignore the exception that occurred in the try block

Answer: a) To handle the exception that occurred in the try block

Explanation: The except block in Python exception handling is used to handle the exception that occurred in the try block. This block is optional and follows the try block. It specifies the type of exception to catch and what code to execute when the exception occurs. Multiple except blocks can be used to catch different types of exceptions. If no exception occurred in the try block, the code in the except block will be skipped.

Q18. Which of the following is NOT true about Python exception handling?

a) It is used to handle unexpected errors that occur during program execution
b) It uses try, except, and finally blocks to manage exceptions
c) It is not recommended to catch and handle all exceptions in a single except block
d) It is recommended to use the assert statement to handle expected errors in the program

Answer: d) It is recommended to use the assert statement to handle expected errors in the program

Explanation: The assert statement in Python is used to raise an exception if a condition is not true. It is typically used to check for programming errors, not expected errors. Python exception handling is used to handle unexpected errors that occur during program execution. It uses try, except, and finally blocks to manage exceptions. It is recommended to catch and handle specific exceptions in separate except blocks and to use the assert statement for programming errors.

Q19. How can you raise your own exception in Python?

a) Using the raise keyword followed by the name of the exception class
b) Using the throw keyword followed by the name of the exception class
c) Using the assert keyword followed by the name of the exception class
d) Using the try keyword followed by the name of the exception class

Answer: a) Using the raise keyword followed by the name of the exception class

Explanation: You can raise your own exception in Python using the raise keyword followed by the name of the exception class. For example, if you want to raise an exception when a certain condition is not met, you can write code like this:

code

if x < 0:
raise ValueError(“x cannot be negative”)
This will raise a ValueError with the message “x cannot be negative” if x is less than 0.

Q20. What happens if an exception is not caught in Python?

a) The program will continue running as if nothing happened
b) The program will terminate with an error message
c) The program will enter an infinite loop
d) The program will prompt the user to handle the exception

Answer: b) The program will terminate with an error message

Explanation: If an exception is not caught in Python, the program will terminate with an error message. The error message will typically include information about the type of exception that occurred, as well as the line number where the exception occurred. It is important to handle exceptions in your code to prevent the program from terminating unexpectedly.

Page : 1           2           3           4