This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 2”.
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
View Answer
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
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
View Answer
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): yield x+1 g=f(8) print(next(g))
a) 8
b) 9
c) 7
d) Error
View Answer
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)
a) Error
b) test
c)
test 10 12
d) No output
View Answer
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 g=f(10) print(next(g)) print(next(g))
a) No output
b)
11 test 12
c)
11 test
d) 11
View Answer
Explanation: 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.
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
View Answer
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]
View Answer
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?
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
View Answer
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
d) finally
View Answer
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’>
View Answer
Explanation: Another way of creating a generator is to use parenthesis. Hence the output of the code shown above is: class<’generator’>.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Programming MCQs
- Apply for Python Internship
- Buy Information Technology Books
- Buy Python Books
- Apply for Programming Internship