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

 

Read More

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!