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