Data structures · 10 sample answers

Python dictionaries: practice questions with answers.

Dictionaries are how Python code stores labelled data: one key, one value, and a lookup that should be fast. The useful skill is not memorising every method; it is knowing what happens when a key is missing.

50 questions in the full bank · 19 choice · 10 fill · 21 output

What this topic tests

  • Read a value safely with get instead of crashing on a missing key.
  • Update, merge, and iterate over keys, values, or pairs.
  • Recognise which objects can be used as dictionary keys.

Common mistakes

  • Using d[key] when the key may be absent. Decide whether get or a default is more appropriate.
  • Changing the dictionary size while iterating over it. Iterate over a list of keys or items instead.
  • Using a list as a key. Dictionary keys must be hashable.
Sample questions

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq0594

    1. Ming looks up a price with menu.get("fruit tea", "not available"). What is returned if fruit tea is not on the menu?

    1. always "not available"
    2. "not available" (when the key is absent)
    3. it deletes the fruit tea key
    4. it adds the fruit tea key
    Show answer

    Answer

    B. "not available" (when the key is absent)

    Why: get(key, default) returns the stored value when the key exists and the default when it does not; it never inserts. So a missing fruit tea yields 'not available'.

  2. Multiple choiceq0604

    2. What does d.pop('a') raise when the key is missing and no default is given?

    1. KeyError
    2. None
    3. IndexError
    4. TypeError
    Show answer

    Answer

    A. KeyError

    Why: pop removes a key and returns its value, but a missing key with no default raises KeyError. Passing a second argument makes it return that instead.

  3. Multiple choiceq0611

    3. 🧋 What is 'key' in {'key':1}?

    1. True
    2. False
    3. 1
    4. 'key'
    Show answer

    Answer

    A. True

    Why: The in operator checks keys, and 'key' is the key of this dictionary, so the expression is True. The value 1 is irrelevant here.

  4. Multiple choiceq0625

    4. In what order do ordinary Python 3.7+ dictionaries store keys?

    1. insertion order
    2. alphabetical order
    3. random order
    4. value order
    Show answer

    Answer

    A. insertion order

    Why: Since Python 3.7 the language guarantees insertion order for dict, so iteration follows the order keys were added. Alphabetical order requires sorted().

  5. Fill in the blankq0596

    5. Test whether “bubble tea” is in menu dictionary d: "bubble tea" ___ d

    Show answer

    Answer

    in

    Why: in tests keys, so 'bubble tea' in d is True only if that string is a key; it does not look at the values. Membership never inspects values.

  6. Fill in the blankq0607

    6. 🧋 Safely read a dictionary value: d.___('key', 'default')

    Show answer

    Answer

    get

    Why: get takes the key first and the fallback second, so d.get('key', 'default') never raises. Without the fallback it returns None.

  7. Fill in the blankq0626

    7. Make an independent copy of the menu. Which method goes in the blank?

    backup = menu.____()
    Show answer

    Answer

    copy

    Why: copy() returns a shallow copy, so adding keys to backup will not touch menu; nested values are still shared. Use deepcopy for nested data.

  8. Output predictionq0595

    8. How do get and setdefault differ, and what is printed?

    d = {"a": 1}
    print(d.get("b", 0), d.setdefault("c", 2), d)
    Show answer

    Answer

    0 2 {'a': 1, 'c': 2}

    Why: get('b', 0) returns 0 without adding b, while setdefault('c', 2) returns 2 and does insert c. Hence 0, 2, and {'a': 1, 'c': 2}.

  9. Output predictionq0618

    9. setdefault inserts the missing b key. What is returned?

    d = {"a": 1}
    print(d.setdefault("b", 2), d)
    Show answer

    Answer

    2 {'a': 1, 'b': 2}

    Why: setdefault returns the existing value if the key is there, otherwise inserts the default and returns it. Here b was missing, so 2 is returned and b appears in d.

  10. Output predictionq0630

    10. What does `score('quirky')` return?

    POINTS = {
        'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1,
        'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8,
        'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1,
        'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1,
        'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4,
        'z': 10
    }
    
    
    def score(word):
        if not word.isalpha():
            return 0
        return sum(POINTS[letter] for letter in word.lower())
    
    print(score('quirky'))
    Show answer

    Answer

    22

    Why: q 10, u 1, i 1, r 1, k 5 and y 4 add up to 22, so score('quirky') returns 22. Nothing is deduplicated, so a repeated letter is counted again.

Common questions

What is the difference between get and square brackets?

d[key] raises KeyError when the key is missing. d.get(key) returns None, and d.get(key, default) returns your fallback.

Do dictionaries keep insertion order?

Current Python versions preserve insertion order as part of the language behaviour, but you should still write code that does not depend on order unless order is part of the requirement.

Keep the streak going.

The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.

Open full practice