wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Review Python Dictionaries

Total questions: 18

Worksheet time: 10mins

Name
Class
Date
1.

You have the following dictionary definition:


d = {'foo': 100, 'bar': 200, 'baz': 300}


What method call will delete the entry whose value is 200 and return 200?

a)

delete d('bar')

b)

d.pop("bar")

c)

d.remove("bar")

d)

None of the above

2.

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':'blue'

'fruit':'apple'

'pet':'dog'

3.

Consider the following statement:


If you only need to work with the values of a dictionary, then you can use .values(), which is a method that returns a list of the dictionary’s values.


Is this statement True or False?

a)

False

b)

True

4.

Items are accessed by their position in a dictionary

a)

True

b)

False

5.

_________________ method will not only delete the item from dictionary, but also return the deleted value.

a)

del ( )

b)

pop( )

c)

Clear( )

d)

erase( )

6.

________________method will returns number of key-value pairs in the given dictionary.

a)

len( )

b)

pop( )

c)

del( )

d)

keys( )

e)

values( )

7.

Which of the following will give an error?

Suppose dict1={"a":1,"b":2,"c":3}

a)

print(len(dict1))

b)

print(dict1.get("b"))

c)

dict1["a"]=5

d)

None of these.

8.

What is the line of code to initialize an empty dictionary called thesaurus?

a)

thesaurus = new Dictionary()

b)

thesaurus = {}

c)

thesaurus = empty_dict()

d)

thesaurus = []

9.

What will the following code output?

a)

"soda"

b)

KeyError

c)

["veggie burger", "salad", "soda"]

d)

["hamburger", "fries", "soda"]

10.

Which code segment when inserted in the blank sets the "Year" from 2006 to 2008?

birthday = {

"Month": "January",

"Day": "01",

"Year":2006

}

__________________________________

OUTPUT: 2008

a)

birthday["Year"]=2008

b)

birthday[Year]=2008

c)

birthday [2008]

d)

birthday[2006]=2008

11.

What will this code do?

birthday = {

"Month": "January",

"Day": "01",

"Year":2006

}

for x, y in birthday.items():

print(x, y)

a)

Prints all the key names in the dictionary 2 at a time

b)

print all the key and value pairs in the dictionary

c)

Prints all the values in the dictionary 2 at a time

12.

Given the dictionary swimmers = {'Manuel':4, 'Lochte':12, 'Adrian':7, 'Ledecky':5, 'Dirado':4}, which of the following lines of code will add an additional key-value pair to the dictionary with "Phelps" as the key and the integer 23 as the value?

a)

swimmers["Phelps"] = 23

b)

swimmers.update({"Phelps":23})

c)

swimmers.setdefault("Phelps",23)

d)

all of the these choices

13.

Given the dictionary d = {'spring': 'autumn', 'autumn': 'fall', 'fall': 'spring'}, what would the following code print?

print (d.get('autumn'))

a)

spring

b)

fall

c)

autumn

d)

none

14.

Given the dictionary d = {'spring': 'autumn', 'autumn': 'fall', 'fall': 'spring'}, what would the following code print?

print (d.get('winter', "spring"))

a)

spring

b)

autumn

c)

none

d)

fall

15.

Given the dictionary d = { 'work': 'success', 'success': 'failure', 'failure': 'money', 'time': 'work', 'industry': 'time'}, in order for this print line to print the word "success", what would need to be the value of x?

print (d[d[x]])

a)

work

b)

industry

c)

time

d)

failure

16.

A dictionary is ordered alphabetically by its keys

a)

true

b)

false

17.

If a dictionary is passed as a parameter to a function, its contents cannot really be changed by the function unless it is returned.

a)

true

b)

false

18.

Consider the dictionary swimmers = {'Manuel':4, 'Lochte':12, 'Adrian':7, 'Ledecky':5, 'Dirado':4}

what is printed with this code:

print(swimmers.get("Adrian") + swimmers.get("Ledecky") + swimmers.get("Phelps", 0))

a)

7 5

b)

"Adrian":"Ledecky"

c)

error

d)

12