Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

EXCEPTION HANDLING IN PYTHON

Total questions: 22

Worksheet time: 10mins

Name
Class
Date
1.

The unexpected event happened during program execution should be handled by

a)

EXCEPTION

b)

EXPERIMENT

c)

ASSERTION

d)

EXTRAORDINARY

2.

try:

a=int(input("\n Enter a Value"))

b=int(input("\n Enter a Value "))

c=a/b

print (c)

except:

print("\n Something Went Wrong")


Assume a = 10 and b = 5, then the output will be

a)

5.0

b)

Something Went Wrong

c)

Arithmetic Error

d)

2.0

3.

Exception disrupts the

a)

Normal flow of the program

b)

Abnornal flow of the program

c)

Hard Disk

d)

None of the Above

4.

The suspicious code is put inside the

a)

Try Block

b)

Finally Block

c)

Else Block

d)

Except Block

5.

________ statement can also be used without specifying Exception.

a)

Try

b)

Finally

c)

Except

d)

Else

6.

try:

a=10/5

print (a)

except ArithmeticError:

print ("This statement is raising an exception" )

finally:

print (" final block executed " )

a)

2.0

final block executed

b)

This statement is raising an exception

final block executed

c)

final block executed

d)

2.0

7.

An exception can be manually triggered by the command

a)

raise

b)

try

c)

except

d)

trigger

8.

n= int(input("Please enter an integer: "))

if n < 0:

raise Exception (" Only Positive Numbers are Allowed ")

print ("Great, you successfully entered an integer!")


what is the output if the given input is -12

a)

Great, you successfully entered an integer!

b)

Only Positive Numbers are Allowed

c)

Only Positive Numbers are Allowed

Great, you successfully entered an integer!

d)

None of the above Option

9.

User defined Exceptions are created from the _________ Parent Class

a)

Exception

b)

Except

c)

Handler

d)

User Defined Exception

10.

How many except statements can a try-except block have?

a)

zero

b)

one

c)

more than one

d)

more than zero

11.

When will the else part of try-except-else be executed?

a)

a) always

b)

b) when an exception occurs

c)

c) when no exception occurs

d)

d) when an exception occurs in to except block

12.

Is the following Python code valid?


try:

# Do something

except:

# Do something

else:

# Do something

a)

no, there is no such thing as else

b)

no, else cannot be used with except

c)

no, else must come before except

d)

yes

13.

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 above Mentioned

14.

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

15.

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

a)

accept

b)

finally

c)

try

d)

except

16.

What will be the output of the following Python code?


lst = [1, 2, 3]

lst[3]

a)

a) NameError

b)

b) ValueError

c)

c) IndexError

d)

d) TypeError

17.

What will be the output of the following Python code?


lst = [1, 2, 3]

lst[3]

print(t[5])

a)

ValuError

b)

TypeError

c)

IndexError

d)

NameError

18.

What will be the output of the following Python code


print(4 + '3')

a)

TypeError

b)

NameError

c)

IndexError

d)

ValueError

19.

CODE 1

import math

num=int(input("Enter a number of whose factorial you want to find"))

print(math.factorial(num))

Input for CODE 1 is -12


CODE 2

num=int(input("Enter a number of whose factorial you want to find"))

print(math.factorial(num))


Input for CODE 2 is 12

a)

ValueError, NameError

b)

AttributeError, ValueError

c)

NameError, TypeError

d)

TypeError, ValueError

20.

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

a)

ValueError

b)

ValueError(“Invalid”)

c)

Invalid

d)

6

21.

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

a)

Bye (printed once)

b)

No output

c)

Invalid (printed once)

d)

Bye (printed infinite number of times)

22.

Which of the following is not a standard exception in Python?

a)

NameError

b)

IOError

c)

AssignmentError

d)

ValueError