Python Exception Handling

Python Exception Handling. Multiple Choice Questions related to Exception Handling in Python Programming Language. This section includes 30+ MCQs on Exception Handling.

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

Exception Handling In Python: MCQs Exception Handling

Exception Handling In Python. MCQs on Exception Handling in Python Programming.

Exception Handling In Python

 

 

Which of the following blocks will be executed whether an exception is thrown or not ??

a) except
b) else
c) finally
d) assert

 

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’>

 

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

 

What exceptions are raised as a result of an error in opening a particular file ??

a) ValueError
b) TypeError
c) ImportError
d) IOError

 

Exception Handling In Python

 

Which of the following is not an exception handling keyword in Python ??

a) try
b) except
c) accept
d) finally

 

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

 

An exception is ??

a) an object
b) a special function
c) a standard module
d) a module

 

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

 

 

Exception Handling In Python

 

 

Syntax errors are also known as parsing errors ??

a) True
b) False

 

What will be the output of the following 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]

 

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

 

Which of the following is not a standard exception in Python ??

a) NameError
b) IOError
c) AssignmentError
d) ValueError

 

Exception Handling In Python

 

 

What will be the output of the following ??

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

 

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

 

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

 

error: Content is protected !!