Python Try Except: Exception Handling MCQs Python
Python Try Except. MCQs on Exception Handling in Python. Try, Except, Finally, and other blocks and their usage with Examples. This section has 20+ Python Try Except Multiple Choice Questions.
Python Try Except
Which of the following statements is true ??
a) The standard exceptions are automatically imported into Python programs
b) All raised standard exceptions must be handled in Python
c) When there is a deviation from the rules of a programming language, a semantic error is thrown
d) If any exception is thrown in try block, else block is executed
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
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
Identify the type of error in the following Python codes ??
Print(“Good Morning”)
print(“Good night)
a) Syntax, Syntax
b) Semantic, Syntax
c) Semantic, Semantic
d) Syntax, Semantic
Python Try Except
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
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
What will be the output of the following Python code if the input entered is 6 ??
valid = False
while not valid:
try:
n=int(input(“Enter a number”))
while n%2==0:
print(“Bye”)
valid = True
except ValueError:
print(“Invalid”)
a) Bye (printed once)
b) No output
c) Invalid (printed once)
d) Bye (printed infinite number of times)
What will be the output of the following ??
#generator
def f(x):
yield x+1
g=f(8)
print(next(g))
a) 8
b) 9
c) 7
d) Error
Python Try Except
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
What will be the output ??
def getMonth(m):
if m<1 or m>12:
raise ValueError(“Invalid”)
print(m)
getMonth(6)
a) ValueError
b) Invalid
c) 6
d) ValueError(“Invalid”)
What will be the output of the ??
x=10
y=8
assert x>y, ‘X too small’
a) Assertion Error
b) 10 8
c) No output
d) 108
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
Python Try Except
Compare the following two Python codes and state the output if the input entered in each case is 6 ??
CODE 1
import math
num=int(input(“Enter a number of whose factorial you want to find”))
print(math.factorial(num))
CODE 2
num=int(input(“Enter a number of whose factorial you want to find”))
print(math.factorial(num))
a) ValueError, NameError
b) AttributeError, ValueError
c) NameError, TypeError
d) TypeError, ValueError
The following Python code will result in an error if the input value is entered as 5 ??
assert False, ‘Spanish’
a) True
b) False
How many except statements can a try-except block have ??
a) zero
b) one
c) more than one
d) more than zero
What will be the output of the ??
int(‘65.43’)
a) ImportError
b) ValueError
c) TypeError
d) NameError
What will be the output of the following Python code if the time module has already been imported ??
4 + ‘3’
a) NameError
b) IndexError
c) ValueError
d) TypeError
What will be the output of the code ??
t[5]
a) IndexError
b) NameError
c) TypeError
d) ValeError
What will be the output of the following Python ??
lst = [1, 2, 3]
lst[3]
a) NameError
b) ValueError
c) IndexError
d) TypeError
What happens if the file is not found in the following Python code ??
a=False
while not a:
try:
f_n = input(“Enter file name”)
i_f = open(f_n, ‘r’)
except:
print(“Input file not found”)
a) No error
b) Assertion error
c) Input output error
d) Name error