0% found this document useful (0 votes)
14 views8 pages

Python Exception Handling Quiz

The document contains a series of multiple-choice questions and answers related to exception handling in Python. It covers topics such as try-except blocks, raising exceptions, handling multiple exceptions, and the use of finally blocks. Each question is followed by the correct answer indicated with a checkmark.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Python Exception Handling Quiz

The document contains a series of multiple-choice questions and answers related to exception handling in Python. It covers topics such as try-except blocks, raising exceptions, handling multiple exceptions, and the use of finally blocks. Each question is followed by the correct answer indicated with a checkmark.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1. What will be the output of the following code?

python
Copy
Edit
try:
x = int("abc")
except ValueError:
print("ValueError caught")
A. No output
B. Runtime Error
C. ValueError caught
D. abc

✅ Answer: C

2. Which of the following best describes the purpose of using try and except blocks
in Python?

A. To catch syntax errors


B. To catch and handle runtime errors
C. To optimize code performance
D. To execute code faster

✅ Answer: B

3. What will the following code print?

python
Copy
Edit
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
A. 0
B. Cannot divide by zero
C. ZeroDivisionError
D. Program crashes

✅ Answer: B

4. Consider this code:

python
Copy
Edit
try:
result = 5 / 0
except:
print("Error occurred")
What is the main drawback of this approach?

A. Code is too slow


B. Syntax error
C. Catching all exceptions hides bugs
D. Division is not possible

✅ Answer: C
5. Choose the correct syntax to handle an exception in Python.

A. catch(Exception e)
B. try { ... } catch(Exception e)
C. try: ... except Exception:
D. handle Exception:

✅ Answer: C

🔁 Topic 2: Multiple Exception Handlers


6. What will this code print?

python
Copy
Edit
try:
x = int("abc")
y = 10 / 0
except ValueError:
print("Value Error")
except ZeroDivisionError:
print("Zero Division Error")
A. Value Error
B. Zero Division Error
C. Both Value Error and Zero Division Error
D. No output

✅ Answer: A

7. Which of the following is true about multiple except blocks?

A. Python runs all applicable except blocks


B. Only the first matching except block is executed
C. All blocks run sequentially
D. Multiple exceptions cannot be handled

✅ Answer: B

8. What is the output of this code?

python
Copy
Edit
try:
a = int("five")
except (ValueError, TypeError):
print("Caught ValueError or TypeError")
A. Error
B. Caught ValueError or TypeError
C. No output
D. Caught Exception

✅ Answer: B

9. Which combination correctly handles both IndexError and KeyError?

A. except IndexError or KeyError:


B. except (IndexError, KeyError):
C. catch IndexError, KeyError:
D. except IndexError | KeyError:

✅ Answer: B

10. Identify the bug:

python
Copy
Edit
try:
lst = [1, 2]
print(lst[5])
except KeyError:
print("Key error")
except ValueError:
print("Value error")
A. Correct
B. IndexError not caught
C. ValueError should be TypeError
D. Missing finally block

✅ Answer: B

🚨 Topic 3: Raising Exceptions


11. What does raise ValueError("Invalid input") do?

A. Logs the error


B. Silently fails
C. Throws a ValueError exception
D. Returns a value

✅ Answer: C

12. What is the output of this code?

python
Copy
Edit
def test(x):
if x < 0:
raise Exception("Negative value")

test(-5)
A. No output
B. -5
C. Negative value
D. Exception is raised

✅ Answer: D

13. Choose the correct syntax for raising a custom exception.

A. throw Exception("Error!")
B. raise new Exception("Error!")
C. raise Exception("Error!")
D. [Link]("Error!")

✅ Answer: C
14. Which statement is true about the raise keyword?

A. It stops the program immediately


B. It can only be used inside except blocks
C. It must be followed by a class or instance
D. It only works with built-in exceptions

✅ Answer: C

15. What happens if you raise an exception but do not catch it?

A. It is ignored
B. Program halts with traceback
C. A warning is logged
D. Python retries the code

✅ Answer: B

🔧 Topic 4: Exceptions with Functions


16. What is the output of this code?

python
Copy
Edit
def divide(a, b):
return a / b

try:
divide(10, 0)
except ZeroDivisionError:
print("Handled in main")
A. Handled in main
B. ZeroDivisionError
C. None
D. 0

✅ Answer: A

17. How can a function pass an exception back to the caller?

A. Use raise in the function


B. Return the exception object
C. Print an error message
D. Use except instead of raise

✅ Answer: A

18. What is the output?

python
Copy
Edit
def risky():
raise ValueError("Oops")

try:
risky()
except ValueError as e:
print(e)
A. risky
B. Oops
C. ValueError
D. Nothing

✅ Answer: B

19. What will happen here?

python
Copy
Edit
def func():
raise Exception("Error in func")

try:
func()
except:
print("Exception handled")
A. Error in func
B. Nothing
C. Exception handled
D. Program crashes

✅ Answer: C

20. Identify the issue:

python
Copy
Edit
def add(x, y):
try:
return x + y
except:
print("Error")

add(5, "2")
A. Correct output
B. No exception occurs
C. TypeError handled but value not returned
D. Result is 52

✅ Answer: C

🧹 Topic 5: Using finally to clean up


21. What does the finally block do?

A. Runs only if an exception occurs


B. Never runs if error occurs
C. Always runs regardless of exception
D. Only runs with return statement

✅ Answer: C

22. What will this code print?

python
Copy
Edit
try:
1 / 0
except:
print("Caught")
finally:
print("Finally block")
A. Finally block
B. Caught
C. Caught
Finally block
D. Error

✅ Answer: C

23. Will finally run after return in try?

python
Copy
Edit
def f():
try:
return "Try"
finally:
print("Finally")

print(f())
A. Try
B. Finally
C. Finally
Try
D. Error

✅ Answer: C

24. What is the purpose of using finally in exception handling?

A. Handling errors
B. Returning from a function
C. Cleaning up resources
D. Catching undefined variables

✅ Answer: C

25. Choose the correct structure:

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

✅ Answer: A

🧩 Mixed Problem Solving (Advanced Use)


26. Predict the output:

python
Copy
Edit
def calculate(x):
try:
return 10 / x
except ZeroDivisionError:
return "Zero error"
finally:
print("End")

print(calculate(0))
A. Zero error
B. End
Zero error
C. Error
D. End

✅ Answer: B

27. What will happen here?

python
Copy
Edit
try:
raise ValueError("Bad value")
except ValueError:
print("Caught")
finally:
raise Exception("New Error")
A. Caught
New Error
B. Caught
C. Error only
D. New Error only

✅ Answer: A

28. What does this code demonstrate?

python
Copy
Edit
try:
print("start")
x = int("bad")
except:
print("error")
finally:
print("done")
A. Uncaught error
B. Only "start" printed
C. start → error → done
D. Crashes at line 3

✅ Answer: C

**29. In which situation is the `


Ask ChatGPT
You said:
please continue

ChatGPT said:
Certainly! Here's the continuation of the remaining multiple-choice questions:

29. In which situation is the finally block skipped?

A. When an exception is raised


B. When return is used inside try
C. When os._exit(0) is called
D. When an exception is caught

✅ Answer: C

Explanation: os._exit(0) terminates the process immediately without running finally


blocks.

30. What is the output of this function?

python
Copy
Edit
def mystery():
try:
return 1
finally:
return 2

print(mystery())
A. 1
B. 2
C. Error
D. None

✅ Answer: B

You might also like