wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Data types & Exceptions

Total questions: 14

Worksheet time: 47mins

Name
Class
Date
1.

What will be the output of the following Python code?

l1=[2,4,6]

l2=[-2,-4,-6]

for i in zip(l1, l2):

print(i)

a)

2, -2

4, -4

6, -6

b)

[(2, -2), (4, -4), (6, -6)]

c)

(2, -2)

(4, -4)

(6, -6)

d)

[-4, -16, -36]

2.

What will be the output of the following Python code?

a=[1,2,3]

b=a.append(4)

print(a)

print(b)

a)

[1,2,3,4]

[1,2,3,4]

b)

[1, 2, 3, 4]

None

c)

Syntax error

d)

[1,2,3]

[1,2,3,4]

3.

What is the output of the following code

aTuple = (100, 200, 300, 400, 500)

aTuple[1] = 800

print(aTuple)

a)

TypeError


b)

(100, 800, 200, 300, 400, 500)

c)

(800, 100, 200, 300, 400, 500)

4.

What is the output of the following tuple operation

aTuple = (100,)

print(aTuple * 2)

a)

TypeError

b)

(100, 100)

c)

(200)

5.

Choose the correct way to access value 20 from the following tuple

aTuple = ("Orange", [10, 20, 30], (5, 15, 25))

a)

aTuple[1:2][1]

b)

aTuple[1:2](1)

c)

aTuple[1:2][1]

d)

aTuple[1][1]

6.

What is the output of the following code

aTuple = (100, 200, 300, 400, 500)

print(aTuple[-2])

print(aTuple[-4:-1])

a)

IndexError: tuple index out of range

b)

400

(200, 300, 400)

c)

(200,300,400)

d)

300,400,500

7.

Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?

a)

d.size()

b)

len(d)

c)

size(d)

d)

d.len()

8.

Consider the following list of pan card numbers:

pancard_list=["AABGT6715H", "UFFAC4352T", "IFSBD9163K", "JOOEC1225H","RWXAFE187B"]

a)

9 OEC1225H

b)

2 AFE187B

c)

9163K O

d)

225H A

9.

What is the output of the code given below?


message="welcome to Mysore"

word=message[-7:]

if(word=="mysore"):

print("got it")

else:

message=message[3:14]

print(message)

a)

got it

b)

lcome to Myso

c)

lcome to Mys

d)

come to Myso

e)

come to Mys

10.

Choose the most appropriate function call needed at line 4 to get the output as ‘Result: 4’.

def check_value(message,num):

msg=message[:num]

return len(msg)


#function call statement

print("Result:", result)

a)

result=check_value(‘Infosys’,3)

b)

result=check_value(4,'Infosys‘)

c)

result=check_value('Infosys',len('Infosys'))

d)

result=check_value('Infosys',4)

e)

result=check_value('Infosys',5)

11.

What is the output of the following python code?

balance=1000

amount="300Rs"


def take_card():

print("Take the card out of ATM")

try:

if balance>=int(amount):

print("Withdraw")

else:

print("Invalid amount")

except TypeError:

print("Type Error Occurred")

except ValueError:

print("Value Error Occurred")

except:

print("Some error Occurred")

finally:

take_card()

a)

Value Error Occurred

Take the card out of ATM

b)

Type Error Occurred

Take the card out of ATM

c)

Some Error occured

d)

Invalid amount

12.

Go through the below code and predict the output:

num1=100

num2=0

try:

result=num1/num2

print(result)

except ZeroDivisionError:

print("Zero Division Error Occurred")

a)

100

b)

0

c)

Zero Division Error Occurred

13.

What will be the output of the code given below?


def division(a,b):

try:

return int(a)/b

except TypeError:

print("Type error")

except ValueError:

print("Value error")

finally:

print("Finally")

print("Done")

division('A',10)

a)

Value error

Finally

Done

b)

Type error

Finally

Done

c)

Type error

Finally

d)

Value error

Finally

14.

What will be the output of the below code?def find_sum(a,b):

try:

print(a+c)

except NameError:

print("Function name error")

finally:

print("Sum finally")

try:

find_sum(12,13)

except NameError:

print("Invocation name error")

finally:

print("Invocation finally")

a)

Function name error

Function name error

Sum finally


Function name error

Sum finally

b)

Function name error

Sum finally

Invocation finally

c)

Invocation name error

Invocation finally

d)

Sum finally

Invocation name error

Invocation finally

e)

Invocation name error

Invocation finally