wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Day 9

Total questions: 6

Worksheet time: 3mins

Name
Class
Date
1.
What would be the output of the following code snippet? a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} for key in a_dict: print(key)
a)
'blue' 'apple' 'dog'
b)
color fruit pet
c)
blue apple dog
d)
color' 'fruit' 'pet'
2.
You have the following dictionary definition: d = {'foo': 100, 'bar': 200, 'baz': 300} What method call will delete the entry whose value is 200?
a)
delete d('bar')
b)
d.pop("bar")
c)
d.remove("bar")
d)
None of the above
3.
Dictionary keys must be immutable
a)
True
b)
False
4.
Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression d[“susan”]?
a)
Since “susan” is not a value in the set, Python raises a KeyError exception
b)
It is executed fine and no exception is raised, and it returns None
c)
Since “susan” is not a key in the set, Python raises a KeyError exception
d)
Since “susan” is not a key in the set, Python raises a syntax error
5.
Ouput?
a)
None
b)
KeyError Exception
c)
-1
6.
Which of the following statements create a dictionary?
a)
d = {}
b)
d = {“john”:40, “peter”:45}
c)
d = {40:”john”, 45:”peter”}
d)
All of the mentioned