Review each question and reveal answers to strengthen your understanding
1What will be the output of the following Python code?
#generator
def f(x):
yield x+1
g=f(8)
print(next(g))
✅ Correct Answer: 2
2What will be the output of the following Python code?
def f(x):
yield x+1
print("test")
yield x+2
g=f(9)
✅ Correct Answer: 4
3 What will be the output of the following Python code?
def a():
try:
f(x, 4)
finally:
print('after f')
print('after f?')
a()
✅ Correct Answer: 3
4What 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))
✅ Correct Answer: 1
5The 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))
✅ Correct Answer: 2
6Which of the following is not an exception handling keyword in Python?
✅ Correct Answer: 3
7What will be the output of the following Python code?
g = (i for i in range(5))
type(g)
✅ Correct Answer: 4
8What 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")
✅ Correct Answer: 1
9What will be the output of the following Python code?
lst = [1, 2, 3]
lst[3]
✅ Correct Answer: 3
10What will be the output of the following Python code?
t[5]
✅ Correct Answer: 2
11What will be the output of the following Python code, if the time module has already been imported?
4 + '3'
✅ Correct Answer: 4
12What will be the output of the following Python code?
int('65.43')
✅ Correct Answer: 2
13Compare 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(math.factorial(num))
CODE 2
num=int(input("Enter a number of whose factorial you want to find"))
print(math.factorial(num))
✅ Correct Answer: 1
14What 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)
✅ Correct Answer: 3
15What 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")
✅ Correct Answer: 4
16Identify the type of error in the following Python codes?
Print(“Good Morningâ€)
print(“Good night)
✅ Correct Answer: 2
17Which of the following statements is true?
✅ Correct Answer: 1
18Which of the following is not a standard exception in Python?
✅ Correct Answer: 3
19An exception is _______
✅ Correct Answer: 1
20_______________ exceptions are raised as a result of an error in opening a particular file.
✅ Correct Answer: 4
21Which of the following blocks will be executed whether an exception is thrown or not?