EXCEPTION HANDLING MCQ
1. How many except statements can a try-except block have?
a) zero
b) one
c) more than one
d) more than zero
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
Answer: c
Page 1 of 39
Explanation: 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
Answer: b
Explanation: Refer documentation.
4. Is the following Python code valid?
Page 2 of 39
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
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
6. When is the finally block executed?
Page 3 of 39
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
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
Page 4 of 39
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
Answer: a
Explanation: No error occurs in the try block so 1 is printed. Then the finally block is executed and 2 is printed.
Page 5 of 39
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
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
Page 6 of 39
b) we get a False
c) an TypeError occurs
d) a ValueError occurs
Answer: b
Explanation: It simply evaluates to False and does not raise any exception.
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
Answer: a
Explanation: The code shown above results in an assertion error. The output of the code is:
Traceback (most recent call last):
File “<pyshell#0>”, line 1, in <module>
assert False, ‘Spanish’
AssertionError: Spanish
Page 7 of 39
Hence, this statement is true.
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) No output
d) 108
Answer: c
Explanation: The code shown above results in an error if and only if x<y. However, in the above case, since x>y, there is no error.
Since there is no print statement, hence there is no output.
3. What will be the output of the following Python code?
#generator
def f(x):
Page 8 of 39
yield x+1
g=f(8)
print(next(g))
a) 8
b) 9
c) 7
d) Error
Answer: b
Explanation: 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.
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)
Page 9 of 39
a) Error
b) test
c)
test
10
12
d) No output
Answer: d
Explanation: 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.
5. What will be the output of the following Python code?
def f(x):
yield x+1
print("test")
yield x+2
Page 10 of 39
g=f(10)
print(next(g))
print(next(g))
a) No output
b)
11
test
12
c)
11
test
d) 11
Answer: b
Explanation: The code shown above results in the output:
11
Page 11 of 39
test
12
This is because we have used next(g) twice. Had we not used next, there would be no output.
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
Answer: c
Page 12 of 39
Explanation: This code shown above will result in an error simply because ‘f’ is not defined. ‘try’ and ‘finally’ are keywords used in
exception handling.
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]
Answer: a
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].
8. The error displayed in the following Python code is?
Page 13 of 39
import itertools
l1=(1, 2, 3)
l2=[4, 5, 6]
l=[Link](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
Answer: b
Explanation: 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.
9. Which of the following is not an exception handling keyword in Python?
a) try
b) except
c) accept
Page 14 of 39
d) finally
Answer: c
Explanation: The keywords ‘try’, ‘except’ and ‘finally’ are exception handling keywords in python whereas the word ‘accept’ is not
a keyword at all.
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’>
Answer: d
Explanation: Another way of creating a generator is to use parenthesis. Hence the output of the code shown above is:
class<’generator’>.
Page 15 of 39
1. 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
Answer: a
Explanation: In the code shown above, if the input file in not found, then the statement: “Input file not found” is printed on the
screen. The user is then prompted to reenter the file name. Error is not thrown.
2. What will be the output of the following Python code?
Page 16 of 39
lst = [1, 2, 3]
lst[3]
a) NameError
b) ValueError
c) IndexError
d) TypeError
Answer: c
Explanation: The snippet of code shown above throws an index error. This is because the index of the list given in the code, that is,
3 is out of range. The maximum index of this list is 2.
3. What will be the output of the following Python code?
t[5]
a) IndexError
b) NameError
c) TypeError
d) ValeError
Page 17 of 39
Answer: b
Explanation: The expression shown above results in a name error. This is because the name ‘t’ is not defined.
4. 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
Answer: d
Explanation: The line of code shown above will result in a type error. This is because the operand ‘+’ is not supported when we
combine the data types ‘int’ and ‘str’. Sine this is exactly what we have done in the code shown above, a type error is thrown.
5. What will be the output of the following Python code?
int('65.43')
a) ImportError
Page 18 of 39
b) ValueError
c) TypeError
d) NameError
Answer: b
Explanation: The snippet of code shown above results in a value error. This is because there is an invalid literal for int() with base
10: ’65.43’.
6. Compare the following two Python codes shown below 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([Link](num))
CODE 2
num=int(input("Enter a number of whose factorial you want to find"))
print([Link](num))
a) ValueError, NameError
Page 19 of 39
b) AttributeError, ValueError
c) NameError, TypeError
d) TypeError, ValueError
Answer: a
Explanation: The first code results in a ValueError. This is because when we enter the input as -6, we are trying to find the factorial
of a negative number, which is not possible. The second code results in a NameError. This is because we have not imported the
math module. Hence the name ‘math’ is undefined.
7. What will be the output of the following Python code?
def getMonth(m):
if m<1 or m>12:
raise ValueError("Invalid")
print(m)
getMonth(6)
a) ValueError
b) Invalid
c) 6
Page 20 of 39
d) ValueError(“Invalid”)
Answer: c
Explanation: In the code shown above, since the value passed as an argument to the function is between 1 and 12 (both included),
hence the output is the value itself, that is 6. If the value had been above 12 and less than 1, a ValueError would have been thrown.
8. 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
Page 21 of 39
c) Invalid (printed once)
d) Bye (printed infinite number of times)
Answer: d
Explanation: The code shown above results in the word “Bye” being printed infinite number of times. This is because an even
number has been given as input. If an odd number had been given as input, then there would have been no output.
9. 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
Answer: b
Explanation: The first code shows an error detected during execution. This might occur occasionally. The second line of code
represents a syntax error. When there is deviation from the rules of a language, a syntax error is thrown.
Page 22 of 39
10. 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
Answer: a
Explanation: When any exception is thrown in try block, except block is executed. If exception in not thrown in try block, else block
is executed. When there is a deviation from the rules of a programming language, a syntax error is thrown. The only true statement
above is: The standard exceptions are automatically imported into Python programs.
11. Which of the following is not a standard exception in Python?
a) NameError
b) IOError
c) AssignmentError
d) ValueError
Answer: c
Explanation: NameError, IOError and ValueError are standard exceptions in Python whereas Assignment error is not a standard
exception in Python.
Page 23 of 39
12. Syntax errors are also known as parsing errors.
a) True
b) False
Answer: a
Explanation: Syntax errors are known as parsing errors. Syntax errors are raised when there is a deviation from the rules of a
language. Hence the statement is true.
13. An exception is ____________
a) an object
b) a special function
c) a standard module
d) a module
Answer: a
Explanation: An exception is an object that is raised by a function signaling that an unexpected situation has occurred, that the
function itself cannot handle.
14. _______________________ exceptions are raised as a result of an error in opening a particular file.
a) ValueError
Page 24 of 39
b) TypeError
c) ImportError
d) IOError
Answer: d
Explanation: IOError exceptions are raised as a result of an error in opening or closing a particular file.
15. Which of the following blocks will be executed whether an exception is thrown or not?
a) except
b) else
c) finally
d) assert
Answer: c
Explanation: The statements in the finally block will always be executed, whether an exception is thrown or not. This clause is used
to close the resources used in a code.
Q. 1You have not given any answer for this question
Is the following code is vaild ?
Page 25 of 39
try:
1/0
except:
print('exception')
else:
print('else')
finally:
print('finally')
Options:
A.
Yes
B.
NO, finally cannot be used with except
C.
NO, else cannot be used with exept
D.
No else and finally cannot be used together
Explanation
Correct answer is : A
Yes. This is a valid
Q. 2You have not given any answer for this question
How many except statements can a try-except block have?
Options:
Page 26 of 39
A.
one
B.
zero
C.
more than zero
D.
more than one
Explanation
Correct answer is : C
try must have one except statement and you can use more than one except statement
Q. 3You have not given any answer for this question
When is the finally block statements executed?
Options:
A.
When a specific condition is satisfied
B.
When there is an exception in try block
C.
When there is no exception in try block
D.
always
Explanation
Page 27 of 39
Correct answer is : D
finally block is executed everytime irrespective of exception occurs
Q. 4You have not given any answer for this question
What is the output of the following code ?
try:
if '2' != 2:
raise ValueError
else:
print('same')
except ValueError:
print('ValueError')
except NameError:
print('NameError')
Options:
A.
ValueError
B.
NameError
C.
same
D.
None of the above
Explanation
Page 28 of 39
Correct answer is : A
The "2" is not same as 2 if block will be executed and it will raise ValueError.
As we kept except ValueError to catch the exception, print('ValueError') will be executed
Q. 5You have not given any answer for this question
What is the output of the following code ?
try:
print('try')
except ValueError:
print('ValueError')
finally:
print('finally')
Options:
A.
try
B.
finally
C.
ValueError
D.
try
finally
Explanation
Page 29 of 39
Correct answer is : D
try block will be executed first and it will print "try"
As finally block is always executed, it will print "finally" also
Q. 6You have not given any answer for this question
When does the else part of try-except-else be executed ?
Options:
A.
When there is a exception occurs in try block
B.
When there is no exception occurs in try block
C.
When there is a exception occurs in except block
D.
always
Explanation
Correct answer is : B
When there is no exception occurs in try block , else block is executed.
Q. 7You have not given any answer for this question
What is the output of the following code ?
a = 20
b = 30
Page 30 of 39
assert b - a , 'a is smaller than b'
print(b)
Options:
A.
30
B.
20
C.
10
D.
AssertionError: a is smaller than b
Explanation
Correct answer is : A
assert keyword raises AssertionError if the statement after assert becomes false.
b - a results in 10, its value is True. So there will be no AssertionError and print(b) will be executed.
As there is no change in value of b, it will print 30
Q. 8You have not given any answer for this question
If there is an error in opening a file, which exception is raised ?
Options:
A.
IndexError
B.
Page 31 of 39
IOError
C.
ImportError
D.
TypeError
Explanation
Correct answer is : B
If there is an error in opening a file, which IOError exception is raised.
If file is not available in the location, then FileNotFoundError exception is raised
Q. 9You have not given any answer for this question
Which of the following is not a standard exception in Python ?
Options:
A.
FileNotFoundError
B.
NameError
C.
ValueError
D.
AssignmentError
Explanation
Correct answer is : D
AssignmentError is not a built in standard error in Python
Page 32 of 39
Q. 10You have not given any answer for this question
What is the output of the following code ?
list1 = [4, 5, 6]
print(list1[4])
Options:
A.
TypeError
B.
ValueError
C.
NameError
D.
IndexError
Explanation
Correct answer is : D
Index value 4 is not available in the list, it will raise IndexError
1. Which block lets you test a block of code for errors?
A. try
B. except
Page 33 of 39
C. finally
D. None of the above
View Answer
Ans : A
Explanation: The try block lets you test a block of code for errors.
2. What will be output for the folllowing code?
try:
print(x)
except:
print(""An exception occurred"")
A. x
B. An exception occurred
C. Error
D. None of the above
View Answer
Ans : B
Explanation: An exception occurred be the output for the followinng code because the try block will generate an error, because x is
not defined.
Page 34 of 39
3. What will be output for the folllowing code?
x = ""hello""
if not type(x) is int:
raise TypeError(""Only integers are allowed"")
A. hello
B. garbage value
C. Only integers are allowed
D. Error
View Answer
Ans : C
Explanation: TypeError: Only integers are allowed
4. What will be output for the folllowing code?
try:
f = open(""[Link]"")
Page 35 of 39
[Link](""Lorum Ipsum"")
except:
print(""Something went wrong when writing to the file"")
finally:
[Link]()
A. [Link]
B. Lorum Ipsum
C. Garbage value
D. Something went wrong when writing to the file
View Answer
Ans : D
Explanation: Something went wrong when writing to the file be output for the folllowing code.
5. Which exception raised when a calculation exceeds maximum limit for a numeric type?
A. StandardError
B. ArithmeticError
C. OverflowError
D. FloatingPointError
View Answer
Ans : C
Page 36 of 39
Explanation: OverflowError : Raised when a calculation exceeds maximum limit for a numeric type
6. Which exception raised in case of failure of attribute reference or assignment?
A. AttributeError
B. EOFError
C. ImportError
D. AssertionError
View Answer
Ans : A
Explanation: AttributeError : Raised in case of failure of attribute reference or assignment.
7. How many except statements can a try-except block have?
A. 0
B. 1
C. more than one
D. more than zero
View Answer
Ans : D
Explanation: There has to be at least one except statement.
Page 37 of 39
8. 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 above
View Answer
Ans : A
Explanation: Each type of exception can be specified directly. There is no need to put it in a list.
9. The following Python code will result in an error if the input value is entered as -5.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: The code shown above results in an assertion error.
10. What will be output for the folllowing code?
x=10
Page 38 of 39
y=8
assert x>y, 'X too small
A. Assertion Error
B. 10 8
C. No output
D. 108
View Answer
Ans : C
Explanation: The code shown above results in an error if and only if xy, there is no error. Since there is no print statement, hence
there is no output.
Page 39 of 39