Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python_Dict_AdvancedQuiz

Total questions: 25

Worksheet time: 19mins

Name
Class
Date
1.
Which of the following types can be used as dictionary keys?
a)
list
b)
set
c)
tuple
d)
dict
2.
What is the result of: d = {'a':1, 'b':2}; d['a']=3; print(d['a'])
a)
1
b)
2
c)
3
d)
Error
3.
What is the output? d = {'x':[1,2], 'y':[3,4]} d['x'].append(5) print(d)
a)
{'x':[1,2], 'y':[3,4]}
b)
{'x':[1,2,5], 'y':[3,4]}
c)
{'x':[5], 'y':[3,4]}
d)
Error
4.
Which method removes and returns a random key-value pair?
a)
pop()
b)
popitem()
c)
remove()
d)
del
5.
Which method inserts a key only if it does not already exist?
a)
update()
b)
setdefault()
c)
insert()
d)
put()
6.
Output? d = {'a':1, 'b':2, 'c':3} print(sorted(d))
a)
['a','b','c']
b)
[1,2,3]
c)
{'a','b','c'}
d)
Error
7.
Which statement copies a dictionary *shallowly*?
a)
d2 = d.copy()
b)
d2 = deepcopy(d)
c)
d2 = dict()
d)
d2 = d
8.
Output? d = {'a':{'x':1,'y':2}} print(d['a']['y'])
a)
1
b)
2
c)
{'x':1,'y':2}
d)
Error
9.
Which expression creates a dictionary from two lists a and b?
a)
dict(a,b)
b)
zip(a,b)
c)
dict(zip(a,b))
d)
pair(a,b)
10.
Find the error: d = {[1,2]: 'numbers'}
a)
list used as key
b)
invalid syntax
c)
missing colon
d)
no error
11.
What is the output? d = {'x':10} x = d x['x']=20 print(d['x'])
a)
10
b)
20
c)
Error
d)
None
12.
Which returns number of key-value pairs in dictionary d?
a)
count(d)
b)
size(d)
c)
len(d)
d)
length(d)
13.
Which will merge dictionaries d1 and d2 into a new dictionary?
a)
d1 + d2
b)
{**d1, **d2}
c)
merge(d1,d2)
d)
append(d1,d2)
14.
Output? d = {'a':1, 'b':2} print('c' not in d)
a)
True
b)
False
c)
None
d)
Error
15.
Which method returns a view object of key-value pairs?
a)
keys()
b)
values()
c)
items()
d)
pairs()
16.
Output? d = {i:i*i for i in range(3)} print(d)
a)
{0:0,1:1,2:4}
b)
{1:1,2:4,3:9}
c)
[0,1,4]
d)
Error
17.
Which will remove a key safely without raising an error?
a)
del d[key]
b)
d.pop(key)
c)
d.pop(key, None)
d)
d.remove(key)
18.
Output? d={'a':1,'b':2} print(list(d.values()))
a)
['a','b']
b)
[1,2]
c)
dict_values(1,2)
d)
Error
19.
Which operator tests key membership?
a)
in
b)
==
c)
is
d)
has
20.
Output? d={'a':[1,2]} e=d.copy() e['a'].append(3) print(d['a'])
a)
[1,2]
b)
[1,2,3]
c)
Error
d)
None
21.
Which expression sorts dictionary d by value ascending?
a)
sorted(d)
b)
sorted(d.values())
c)
sorted(d.items(), key=lambda x:x[1])
d)
sort(d)
22.
Output? d={1:'a',2:'b'} print(d.get(3,'missing'))
a)
None
b)
missing
c)
Error
d)
3
23.
Which of the following creates dictionary with default value 0 for keys in list L?
a)
dict(L)
b)
fromkeys(L,0)
c)
make(L,0)
d)
dict(L,0)
24.
Output? d={'a':1,'b':2} d.update({'b':5}) print(d['b'])
a)
1
b)
2
c)
5
d)
Error
25.
What is True about dictionary ordering (Python 3.7+)?
a)
Unordered
b)
Sorted by key
c)
Maintains insertion order
d)
Random