Home
MCQs by exam
Python Interview Dictionaries
Data Science
Python Interview Dictionaries MCQs – Data Science
24 multiple choice questions typical of the Python Interview Dictionaries exam, with answers and explanations. Switch on practice mode to test yourself first.
print(dict(a=1, b=2))
A
{'a': 1}
B
{'b': 2}
C
Error
D
{'a': 1, 'b': 2}
Correct answer
print(dict.fromkeys('ab', 0))
A
{'a': None}
B
{'a': 0}
C
{'a': 0, 'b': 0}
Correct answer
D
Error
print({'a': 1, 'b': 2}.keys() - {'a': 0}.keys())
A
{'b'}
Correct answer
B
{'a'}
C
{'a', 'b'}
D
Error
print(dict.fromkeys(['a', 'b']))
A
{'a': 0, 'b': 0}
B
['a', 'b']
C
Error
D
{'a': None, 'b': None}
Correct answer
print({'a'}
A
False
B
Error
C
None
D
True
Correct answer
d = {'a': 3, 'b': 1, 'c': 2}
print(sorted(d, key=d.get))
A
['b', 'c', 'a']
Correct answer
B
['a', 'b', 'c']
C
['c', 'b', 'a']
D
Error
print(sorted({'a': 3, 'b': 1, 'c': 2}.values()))
A
[3, 2, 1]
B
[1, 3, 2]
C
[1, 2, 3]
Correct answer
D
Error
d = {'a': 1, 'b': 2}
print(d.popitem())
A
('a', 1)
B
b
C
('b', 2)
Correct answer
D
Error
print({'a': 1, 'b': 2} | {'b': 3})
A
{'a': 1, 'b': 2}
B
{'b': 3}
C
{'a': 1, 'b': 3}
Correct answer
D
Error
A
2.7
B
3.0
C
3.7
Correct answer
D
3.5
print('a' in {'a': 1})
A
False
B
True
Correct answer
C
1
D
Error
d = {'a': 1}
print(d.get('z'))
A
KeyError
B
None
Correct answer
C
0
D
Error
A
Always overwrites the key
B
Returns the value if key exists, otherwise inserts default and returns it
Correct answer
C
Deletes the key
D
Raises KeyError if missing
print(1 in {'a': 1})
A
True
B
1
C
False
Correct answer
D
Error
d = {'a': 1}
print(d.get('z', 0))
A
None
B
KeyError
C
1
D
0
Correct answer
from collections import Counter
print(Counter('aaabbc'))
A
Counter({'a': 3, 'b': 2, 'c': 1})
Correct answer
B
{'a': 3, 'b': 2, 'c': 1} sorted
C
['a', 'b', 'c']
D
6
print(list({'a': 1, 'b': 2}.items()))
A
['a', 'b']
B
[('a', 1), ('b', 2)]
Correct answer
C
[1, 2]
D
{'a': 1}
A
IndexError
B
ValueError
C
KeyError
Correct answer
D
NameError
A
OrderedDict
B
Counter
C
defaultdict
Correct answer
D
deque
d = {'a': 1, 'b': 2}
d.update({'b': 3, 'c': 4})
print(d)
A
{'a': 1, 'b': 2, 'c': 4}
B
{'a': 1, 'b': 2}
C
Error
D
{'a': 1, 'b': 3, 'c': 4}
Correct answer
d = {'a': 1, 'b': 2}
print({k: v for k, v in d.items() if v
A
{'a': 1}
Correct answer
B
{'a': 1, 'b': 2}
C
{}
D
Error
d = {'a': 1, 'b': 2}
print(d.pop('a'))
A
2
B
None
C
1
Correct answer
D
KeyError
A
int
B
list
Correct answer
C
str
D
tuple of ints
A
O(n)
B
O(log n)
C
O(1)
Correct answer
D
O(n log n)
Practise on paper
Preparing for Python Interview Dictionaries?
Make it your exam plan: see your progress in every subject and topic, set a daily goal and practise what you have not done yet.
Start my Python Interview Dictionaries plan
Test yourself
Take a timed online test and get a certificate, or play today's quick quiz.
Online tests
Daily quiz