Python MCQs on Exception Handling : SET 1

Python MCQs on Exception Handling

In this section, we are going to introduce you to the most frequently asked Python MCQs on Exception Handling. These Python MCQs on Exception Handling will be going to help you in Interviews and Exams.

 

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

Check Answer

Answer: d
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

Check Answer

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

3. Is the following Python code valid?

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

Check Answer

Answer: b
Refer documentation.

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

Check Answer

Answer: d
Refer documentation.

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

Check Answer

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

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

Check Answer

Answer: d
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

Check Answer

Answer: b
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

Check Answer

Answer: a
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

Check Answer

Answer: c
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

Check Answer

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

Page  1  2  3

 

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment