Python Multiple Choice Questions – Exception Handling

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling”.

1. How many except statements can a try-except block have?
a) zero
b) one
c) more than one
d) more than zero
View Answer

Answer: d
Explanation: There has to be at least one except statement.

2. When will the else part of try-except-else be executed?
a) always
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block
View Answer

Answer: c
Explanation: The else part is executed when no exception occurs.

3. Is the following Python code valid?

advertisement
advertisement
try:
    # Do something
except:
    # Do something
finally:
    # Do something

a) no, there is no such thing as finally
b) no, finally cannot be used with except
c) no, finally must come before except
d) yes
View Answer

Answer: d
Explanation: Refer documentation.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. Is the following Python code valid?

try:
    # Do something
except:
    # Do something
else:
    # Do something

a) no, there is no such thing as else
b) no, else cannot be used with except
c) no, else must come before except
d) yes
View Answer

Answer: d
Explanation: Refer documentation.
advertisement

5. Can one block of except statements handle multiple exception?
a) yes, like except TypeError, SyntaxError [,…]
b) yes, like except [TypeError, SyntaxError]
c) no
d) none of the mentioned
View Answer

Answer: a
Explanation: Each type of exception can be specified directly. There is no need to put it in a list.
advertisement

6. When is the finally block executed?
a) when there is no exception
b) when there is an exception
c) only if some condition that has been specified is satisfied
d) always
View Answer

Answer: d
Explanation: The finally block is always executed.

7. What will be the output of the following Python code?

def foo():
    try:
        return 1
    finally:
        return 2
k = foo()
print(k)

a) 1
b) 2
c) 3
d) error, there is more than one return statement in a single try-finally block
View Answer

Answer: b
Explanation: The finally block is executed even there is a return statement in the try block.

8. What will be the output of the following Python code?

def foo():
    try:
        print(1)
    finally:
        print(2)
foo()

a) 1 2
b) 1
c) 2
d) none of the mentioned
View Answer

Answer: a
Explanation: No error occurs in the try block so 1 is printed. Then the finally block is executed and 2 is printed.

9. What will be the output of the following Python code?

try:
    if '1' != 1:
        raise "someError"
    else:
        print("someError has not occurred")
except "someError":
    print ("someError has occurred")

a) someError has occurred
b) someError has not occurred
c) invalid code
d) none of the mentioned
View Answer

Answer: c
Explanation: A new exception class must inherit from a BaseException. There is no such inheritance here.

10. What happens when ‘1’ == 1 is executed?
a) we get a True
b) we get a False
c) an TypeError occurs
d) a ValueError occurs
View Answer

Answer: b
Explanation: It simply evaluates to False and does not raise any exception.

More MCQs on Python Exception Handling:

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.