Vidyalelo
Python · all questions

Exception Handling in Python
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

65

Questions

3/4

Page

Pick an option on a question to see the right answer and solution.

What will be the output of the following Python code?
lst = [1, 2, 3]
lst[3]

Select an option to see the answer and solution.

What will be the output of the following Python code?
def a():
    try:
        f(x, 4)
    finally:
        print('after f')
    print('after f?')
a()

Select an option to see the answer and solution.

What will be the output of the following Python code?
x=10
y=8
assert x>y, 'X too small'

Select an option to see the answer and solution.

What will be the output of the following Python code?
#generator
def f(x):
    yield x+1
g=f(8)
print(next(g))

Select an option to see the answer and solution.

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")

Select an option to see the answer and solution.

What is the purpose of the sys.exc_info() function in Python?

Select an option to see the answer and solution.

What will be the output of the following Python code?
def foo():
    try:
        return 1
    finally:
        return 2
k = foo()
print(k)

Select an option to see the answer and solution.

What will happen if an exception is raised in a try block and is not handled in any of the except blocks?

Select an option to see the answer and solution.

How can you create a custom exception class in Python?

Select an option to see the answer and solution.

Which of the following statements is true?

Select an option to see the answer and solution.

Syntax errors are also known as parsing errors.

Select an option to see the answer and solution.

What is the purpose of the assert statement in Python?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Which built-in Python function can be used to retrieve the error message associated with an exception?

Select an option to see the answer and solution.

What is the purpose of the except clause without specifying an exception type in a try block?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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")

Select an option to see the answer and solution.

Is the following Python code valid?
try:
    # Do something
except:
    # Do something
else:
    # Do something

Select an option to see the answer and solution.

What is the purpose of the assert statement in Python?

Select an option to see the answer and solution.

What will be the output of the following Python code, if the time module has already been imported?
4 + '3'

Select an option to see the answer and solution.