Unit-1: Exception Handling (Questions)
Multiple Choice Questions
1. Which of the following is the primary purpose of exception handling in programming?
A. To intentionally crash the program
B. To ignore errors and continue program execution
C. To gracefully manage and recover from unexpected errors
D. To create errors for testing purposes
2. What is the primary role of the `try` block in a try-except construct?
A. To execute code that may raise an exception
B. To handle exceptions
C. To indicate the end of the try-except construct
D. To prevent exceptions from occurring
3. In a try-except block, if an exception occurs, which block is executed?
A. The try block B. The except block
C. Both try and except blocks simultaneous D. None of the above
4. What is the purpose of the `finally` block in exception handling?
A. To specify the types of exceptions to catch
B. To execute code regardless of whether an exception occurred or not
C. To create custom exceptions
D. To prevent exceptions from propagating
5. Which keyword is used to specify the type of exception to catch in an except block?
A. `catch` B. `try` C. `except` D. `handle`
6. Which exception is raised when attempting to access a non-existent file?
A. FileNotFoundError B. FileNotAccessibleError
C. NonExistentFileError D. InvalidFileAccessError
7. What type of error does a `ValueError` exception typically represent in Python?
A. A network-related error B. A division by zero error
C. An error related to invalid data conversion. D. A file handling error
8. What is the main purpose of using a `try-except-finally` construct?
A. To create custom exceptions
B. To ensure that no exceptions are raised
C. To gracefully manage exceptions and execute cleanup code
D. To replace if-else statements
9. What happens if an exception is raised in the `finally` block?
A. The program crashes
B. The exception is caught and handled by the `except` block
C. The program continues executing normally
D. The `finally` block cannot raise exceptions
10. What will be the output of the following code?
try:
print(10 / 0)
except ZeroDivisionError as e:
print("Error:", str(e))
finally:
print("Finally block")
a) Error: Division by zero Finally block b) Error: Index out of range Finally block
c) Error: None Finally block d) The code will raise an exception and terminate.
11. What will be the output of the following code?
try:
print("Hello" + 123)
except TypeError:
print("Error: Type mismatch")
else:
print("No error")
a) Hello123 No error b) Error: Type mismatch No error
c) Error: Type mismatch d) The code will raise an exception and terminate.
Assertion Reason Questions
A. Both assertion and reason are true, and the reason is the correct explanation of the assertion.
B. Both assertion and reason are true, but the reason is not the correct explanation of the assertion.
C. Assertion is true, but the reason is false.
D. Assertion is false, but the reason is true.
1. Assertion: The `try` block is optional in a try-except construct.
Reason: You cannot have an `except` block without a corresponding `try` block. Answer: D
2. Assertion: The `finally` block is executed only if an exception occurs in the `try` block.
Reason: The `except` block is used exclusively for handling exceptions. Answer: D
Short Answer Questions
1. Differentiate between a `try-except` block and a `try-except-finally` block.
Answer: A `try-except` block handles exceptions by catching and handling specific errors but does not
guarantee that certain cleanup operations will be performed. A `try-exceptfinally` block, on the other
hand, ensures that the code within the `finally` block executes, making it suitable for cleanup operations.
2. Write a Python code snippet that demonstrates the use of a `try-except` block to handle the following
scenario: Attempting to open a file named "[Link]" and handling the `FileNotFoundError` by printing
an error message.
Answer:
try:
file = open("[Link]", "r")
except FileNotFoundError:
print("Error: File not found.")
3. Can you provide an example of using the `finally` block to ensure proper resource cleanup? Explain
why it is important.
Answer:
try:
file = open("[Link]", "r")
content = [Link]()
except FileNotFoundError:
print("Error: File not found.")
finally:
[Link]() # Ensures that the file is always closed, even if an exception occurs. It is
important to close the file properly to release resources and avoid potential issues, such as data
corruption.