You are on page 1of 5

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

- A) To terminate the program when an error occurs

- B) To ignore errors and continue program execution

- C) To gracefully handle errors and prevent program crashes

- D) To display errors to the user without taking any action

- Answer: C) To gracefully handle errors and prevent program crashes

2. Which keyword is used to handle exceptions in Python?

- A) try

- B) catch

- C) except

- D) handle

- Answer: C) except

3. In a try-except block, where should the code that might raise an exception be placed?

- A) In the try block

- B) In the except block

- C) In the finally block

- D) In the else block

- Answer: A) In the try block

4. Which of the following statements is true about the "finally" block in Python?

- A) It is executed only if an exception occurs.

- B) It is executed only if no exception occurs.

- C) It is executed regardless of whether an exception occurs or not.

- D) It is not a mandatory part of exception handling.

- Answer: C) It is executed regardless of whether an exception occurs or not.


5. Which of the following exceptions is raised when an attempt is made to access a key that does not
exist in a dictionary?

- A) KeyError

- B) ValueError

- C) IndexError

- D) SyntaxError

- Answer: A) KeyError

6. What does the "raise" keyword do in Python?

- A) It raises an exception manually.

- B) It catches an exception.

- C) It ignores an exception.

- D) It handles an exception

-Answer: A) It raises an exception manually.

7. Which of the following is a built-in Python function used to generate exceptions?

- A) error()

- B) raise()

- C) exception()

- D) throw()

-Answer: B) raise()

8. In Python, how can multiple except blocks be used in exception handling?

- A) Only one except block can be used per try block.

- B) Multiple except blocks can be used, each handling a specific type of exception.

- C) Multiple except blocks can be used, but they must all handle the same type of exception.

- D) Multiple except blocks are not allowed in Python.


- Answer: B) Multiple except blocks can be used, each handling a specific type of exception.

9. What is the purpose of the "else" block in a try-except statement?

- A) To execute code regardless of whether an exception occurs or not.

- B) To execute code only if an exception occurs.

- C) To execute code only if no exception occurs.

- D) To handle exceptions.

-Answer: C) To execute code only if no exception occurs.

10. Which of the following is not a standard Python exception type?

- A) FileNotFoundError

- B) ZeroDivisionError

- C) TypeMismatchError

- D) ValueError

-Answer: C) TypeMismatchError

1.

try:

print("Hello")

raise ValueError("Custom error")

except ValueError as e:

print(e)

finally:

print("World")

Output:

Hello

Custom error

World
**Explanation:** The code prints "Hello", raises a ValueError with a custom message, catches the
ValueError in the except block, prints the error message, and then executes the finally block which
prints "World".

2. try:

num = 10 / 0

print(num)

except ZeroDivisionError:

print("Division by zero!")

Output:

Division by zero!

Explanation: The code attempts to divide by zero, which raises a ZeroDivisionError. This error is
caught in the except block and "Division by zero!" is printed.

3.

try:

num = int("ten")

print(num)

except ValueError:

print("Conversion error!")

finally:

print("End of program")

Output:

Conversion error!

End of program

Explanation: The code attempts to convert the string "ten" to an integer, which raises a ValueError.
This error is caught in the except block and "Conversion error!" is printed. The finally block is always
executed, printing "End of program".

4.

try:
raise Exception("An exception occurred!")

except ValueError:

print("Caught a ValueError")

except Exception as e:

print(e)

Output:

An exception occurred!

Explanation:The code raises an Exception with a custom message. Since there is no specific handler
for Exception, it goes to the general except block, printing the exception message.

5. Question:

try:

with open("nonexistent_file.txt") as f:

content = f.read()

except FileNotFoundError:

print("File not found!")

Predicted Output:

File not found!

Explanation: The code tries to open a file that doesn't exist, which raises a FileNotFoundError. This
error is caught in the except block and "File not found!" is printed.

You might also like