Python Dictionaries MCQs (25)
Python Training in Vizag (Softenant)1) Basic access
d = {'a': 1, 'b': 2}
print(d['b'])
A. 1
B. 2
C. KeyError
D. None
2) Missing key
d = {'a': 1}
print(d['x'])
A. None
B. 0
C. KeyError
D. False
3) Using get with default
d = {'a': 1}
print(d.get('x', 0))
A. None
B. 0
C. KeyError
D. False
4) setdefault builds a container once
d = {}
d.setdefault('a', [])
d['a'].append(1)
print(d['a'])
A. []
B. [1]
C. KeyError
D. None
5) update overrides existing keys
d = {'a': 1}
d.update({'a': 9, 'b': 2})
print(d['a'], d['b'])
A. 1 2
B. 9 2
C. 9 KeyError
D. 1 KeyError
6) pop returns value and removes
d = {'a': 1, 'b': 2}
x = d.pop('b')
print(x, 'b' in d)
A. 2 False
B. 2 True
C. None False
D. KeyError True
7) pop with default
d = {'a': 1}
print(d.pop('b', 0))
A. KeyError
B. 0
C. None
D. False
8) popitem (LIFO in Py 3.7+)
d = {'a': 1, 'b': 2}
k, v = d.popitem()
print(k, v)
A. a 1
B. b 2
C. a 2
D. KeyError
9) Dict comprehension
d = {c: ord(c) for c in "ab"}
print(d)
A. {‘a’: 97, ‘b’: 98}
B. {‘a’: ‘a’, ‘b’: ‘b’}
C. {‘ab’: 195}
D. [‘a’, ‘b’]
10) Views, not lists
d = {'x':1}
ks = d.keys()
print(isinstance(ks, list), type(ks).__name__)
A. True list
B. False dict_keys
C. False list
D. True dict_keys
11) Insertion order preserved
d = {'x':1, 'y':2}
print(list(d.keys()))
A. [‘y’, ‘x’]
B. [‘x’, ‘y’]
C. Random
D. Error
12) Equality ignores order
a = {'a':1, 'b':2}
b = {'b':2, 'a':1}
print(a == b)
A. True
B. False
C. TypeError
D. Depends on Python
13) Unhashable key
d = { [1,2]: 'x' }
A. Works
B. TypeError
KeyError
D. ValueError
14) Tuple key OK
d = { (1,2): 'ok' }
print(d[(1,2)])
A. ok
B. KeyError
TypeError
D. None
15) fromkeys
d = dict.fromkeys('ab', 0)
print(d)
A. {‘ab’: 0}
B. {‘a’: 0, ‘b’: 0}
C. {‘a’: None, ‘b’: None}
D. Error
16) dict from pairs
d = dict([('a',1), ('b',2)])
print(d['b'])
A. 1
B. 2
C. KeyError
D. None
17) Merge operator (|, Py ≥ 3.9)
a = {'x':1}
b = {'x':9, 'y':2}
c = a | b
print(c['x'], c['y'])
A. 1 2
B. 9 2
C. 9 KeyError
D. 1 KeyError
18) In-place merge (|=)
a = {'x':1}
b = {'x':9, 'y':2}
a |= b
print(a['x'], 'y' in a)
A. 1 False
B. 9 True
C. 9 False
D. 1 True
19) Items view is dynamic
d = {'a':1}
it = d.items()
d['b'] = 2
print(('b',2) in it)
A. True
B. False
C. KeyError
D. TypeError
20) Shallow copy
d = {'a':[1]}
c = d.copy()
c['a'].append(2)
print(d['a'])
A. [1]
B. [1, 2]
C. None
D. Raises
21) Clear
d = {'a':1}; d.clear()
print(len(d))
A. 0
B. 1
2
D. Raises
22) Frequency pattern with get
s = "aba"
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
print(freq['a'])
A. 1
B. 2
C. 3
D. KeyError
23) Deleting missing key
d = {'a':1}
del d['x']
A. Does nothing
B. KeyError
C. Returns None
D. False
24) in checks keys, not values
d = {'a':1, 'b':2}
print(2 in d, 2 in d.values())
A. True False
B. False True
C. True True
D. False False
25) Nested access with get
d = {'u': {'v': 3}}
print(d['u'].get('v', 0))
A. 0
B. 3
C. KeyError
D. None
Answer Key
1) B
2) C
3) B
4) B
5) B
6) A
7) B
8) B
9) A
10) B
11) B
12) A
13) B
14) A
15) B
16) B
17) B
18) B
19) A
20) B
21) A
22) B
23) B
24) B
25) B