Python Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 2”.
Q 1. The following Python code will result in an error if the input value is entered as -5.
assert False, ‘Spanish’
A. True
B. False
Show Answer
Answer:-A. TrueExplanation
The code shown above results in an assertion error. The output of the code is: Traceback (most recent call last): File “
Q 2. What will be the output of the following Python code?
x=10
y=8
assert x>y, ‘X too small’
A. Assertion Error
B. 10 8
C. 108
D. No output
Show Answer
Answer:-D. No outputExplanation
The code shown above results in an error if and only if xQ 3. What will be the output of the following Python code?
#generator
def f(x):
yield x+1
g=f(8)
print(next(g))
A. 8
B. 9
C. 7
D. Error
Show Answer
Answer:-B. 9Explanation
The code shown above returns the value of the expression x+1, since we have used to keyword yield. The value of x is 8. Hence the output of the code is 9.
Q 4. What will be the output of the following Python code?
def f(x):
yield x+1
print(“test”)
yield x+2
g=f(9)
A. Error
B. test
C. test
10
12
D. No output
Show Answer
Answer:-D. No outputExplanation
The code shown above will not yield any output. This is because when we try to yield 9, and there is no next(g), the iteration stops. Hence there is no output.
Q 5. What will be the output of the following Python code?
def f(x):
yield x+1
print(“test”)
yield x+2
g=f(10)
print(next(g))
print(next(g))
A. No output
B.11
test
12
C. 11
test
D. 11
Show Answer
Answer:-B.11 test 12Explanation
The code shown above results in the output: 11 test 12 This is because we have used next(g) twice. Had we not used next, there would be no output.
Q 6. What will be the output of the following Python code?
def a():
try:
f(x, 4)
finally:
print(‘after f’)
print(‘after f?’)
a()
A. No output
B. after f?
C. error
D. after f
Show Answer
Answer:-C. errorExplanation
This code shown above will result in an error simply because ‘f’ is not defined. ‘try’ and ‘finally’ are keywords used in exception handling.
Q 7. What will be the output of the following Python code?
def f(x):
for i in range(5):
yield i
g=f(8)
print(list(g))
A. [0, 1, 2, 3, 4]
B. [1, 2, 3, 4, 5, 6, 7, 8]
C. [1, 2, 3, 4, 5]
D. [0, 1, 2, 3, 4, 5, 6, 7]
Show Answer
Answer:-A. [0, 1, 2, 3, 4]Explanation
The output of the code shown above is a list containing whole numbers in the range (5). Hence the output of this code is: [0, 1, 2, 3, 4].
Q 8. The error displayed in the following Python code is?
import itertools
l1=(1, 2, 3)
l2=[4, 5, 6]
l=itertools.chain(l1, l2)
print(next(l1))
A. ‘list’ object is not iterator
B. ‘tuple’ object is not iterator
C. ‘list’ object is iterator
D. ‘tuple’ object is iterator
Show Answer
Answer:-B. ‘tuple’ object is not iteratorExplanation
The error raised in the code shown above is that: ‘tuple’ object is not iterator. Had we given l2 as argument to next, the error would have been: ‘list’ object is not iterator.
Q 9. Which of the following is not an exception handling keyword in Python?
A. try
B. except
C. accept
D. finally
Show Answer
Answer:-C. acceptExplanation
The keywords ‘try’, ‘except’ and ‘finally’ are exception handling keywords in python whereas the word ‘accept’ is not a keyword at all.
Q 10. What will be the output of the following Python code?
g = (i for i in range(5))
type(g)
A. class <’generator’>
B. class <‘iteration’>
C. class <’range’>
D. class <’loop’>
Show Answer
Answer:-A. class <’generator’>Explanation
10. What will be the output of the following Python code? g = (i for i in range(5)) type(g) a) class <’loop’> b) class <‘iteration’> c) class <’range’> d) class <’generator’> View Answer Answer: d Explanation: Another way of creating a generator is to use parenthesis. Hence the output of the code shown above is: class<’generator’>.Q 11. What’s the output of the following Python code?
Python
def div_by_zero(x, y):
try:
return x / y
except ZeroDivisionError:
print("Error: Can't divide by zero")
return None
print(div_by_zero(10, 2))
print(div_by_zero(10, 0))
A. 5.0 Error: Can't divide by zero None
B. 5.0 Error: Can't divide by zero
C. 5.0 None Error: Can't divide by zero
D. Error: Can't divide by zero 5.0
Show Answer
A. 5.0 Error: Can’t divide by zero NoneExplanation
The first call, div_by_zero(10, 2), executes the try block successfully and returns 5.0. The second call, div_by_zero(10, 0), triggers a ZeroDivisionError, which is caught by the except block. This block prints the error message “Error: Can’t divide by zero” and then returns None. The final two print statements in the code display the return values from the two function calls.Q 12. Which of the following statements about Python’s try...except...finally block is true?
A. The finally block is only executed if an exception occurs.
B. The except block is always executed, even if no exception occurs.
C. The finally block always executes, regardless of whether an exception occurred or was handled.
D. The finally block is optional and is used to clean up resources, but it doesn’t run if an exception is raised.
Show Answer
C. The finally block always executes, regardless of whether an exception occurred or was handled.Explanation
The finally block is guaranteed to execute. It’s used for cleanup actions like closing files or releasing network connections, ensuring these actions happen regardless of whether the code in the try block succeeds or fails.Q 13. What does the else block in a try...except...else statement do?
A. It executes when an exception is handled by the except block.
B. It executes if the try block is executed without any exceptions.
C. It executes only when the finally block is not present.
D. It is a placeholder and has no functional purpose.
Show Answer
B. It executes if the try block is executed without any exceptions.Explanation
The else block in a try…except statement is optional and runs only if the code in the try block executes successfully and no exception is raised. It’s often used to contain code that should run only after the try block’s success.Q 14. What’s the output of the following code?
Python
try:
print("Trying...")
raise TypeError
except NameError:
print("Caught NameError")
except TypeError:
print("Caught TypeError")
finally:
print("Finally!")
A. Trying... Caught NameError Finally!
B. Trying... Finally!
C .Trying... Caught TypeError Finally!
D .Trying... Caught TypeError
Show Answer
C .Trying… Caught TypeError Finally!Explanation
The try block starts executing and prints “Trying…”. The raise TypeError statement immediately raises a TypeError exception. Python then looks for an except block that can handle this specific exception. It finds except TypeError, so it executes the code inside that block, printing “Caught TypeError”. After the exception is handled, the finally block is executed, printing “Finally!”.Q 15. In Python, is it possible to have an except block without a specific exception name?
A. No, every except block must specify an exception.
B. Yes, except with no exception name catches all exceptions, but it’s generally considered bad practice.
C. Yes, except with no exception name is the preferred way to catch all exceptions.
D. No, this will cause a syntax error.


Leave a Reply