Python MCQs on Exception Handling : SET 2

11. The following Python code will result in an error if the input value is entered as -5.

assert False, ‘Spanish’

a) True

b) False

Check Answer

Answer: a
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
Hence, this statement is true.

12. 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

Check Answer

Answer: c
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.

13. 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

Check Answer

Answer: b
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.

14. 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

Check Answer

Answer: d
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.

15. 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

Check Answer

Answer: b
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.

16. 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

Check Answer

Answer: c
This code shown above will result in an error simply because ‘f’ is not defined. ‘try’ and ‘finally’ are keywords used in exception handling.

17. 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]

Check Answer

Answer: a
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].

18. 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

Check Answer

Answer: b
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.

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

a) try

b) except

c) accept

d) finally

Check Answer

Answer: c
The keywords ‘try’, ‘except’ and ‘finally’ are exception handling keywords in python whereas the word ‘accept’ is not a keyword at all.

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

Check Answer

Answer: d
Another way of creating a generator is to use parenthesis. Hence the output of the code shown above is: class<’generator’>.

Page  1  2  3

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment