Dictionaries — Hash Maps Done Right — Concept Check
Questions on Dictionaries — Hash Maps Done Right. Dicts are Python's hash maps: O(1) average lookup, insertion-ordered since 3.7, with get/setdefault patterns that eliminate KeyError boilerplate.
Questions in this quiz
8 questions on Dictionaries — Hash Maps Done Right — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses d[key] to access a dictionary element that might be missing. What is the consequence?
- AReturns None if the key is missing
- BRaises a KeyError if the key is missing
- CReturns a default value if the key is missing
- DInserts the key with a default value
2.In Python 3.7+, what is a key characteristic of dictionaries?
- AThey automatically sort keys
- BThey preserve insertion order
- CThey do not allow duplicate keys
- DThey are immutable
3.You want to count the frequency of characters in a string using a dictionary. Which method avoids KeyError?
- AUsing a non-hashable key
- BUsing a list as a key
- CDirectly accessing the key
- DUsing get() with a default value
4.When merging two dictionaries in Python 3.9+, what happens if there are conflicting keys?
- AThe right-side dictionary's value is used
- BBoth values are stored as a list
- CAn error is thrown
- DThe left-side dictionary's value is used
5.A developer mistakenly believes that using setdefault() always modifies the dictionary. What is the correct behavior?
- Asetdefault() deletes the key if present
- Bsetdefault() only modifies if the key is missing
- Csetdefault() always modifies the dictionary
- Dsetdefault() raises an error if the key exists
6.A dictionary has keys that are tuples. Why is this allowed?
- ATuples are converted to strings internally
- BTuples are mutable and can be keys
- CTuples automatically sort themselves
- DTuples are hashable and can be used as keys
7.What is the result of using d.get('key', 0) when 'key' is not in the dictionary?
- AInserts 'key' with value 0
- BRaises a KeyError
- CReturns None
- DReturns 0
8.A developer uses a list as a key in a dictionary. What happens?
- AThe dictionary accepts the list
- BThe list is converted to a tuple
- CThe list is converted to a string
- DTypeError: unhashable type: 'list'