Quiz: Chapter 9 :Python Data Structures (Python for Everybody Specialization) Answers 2025
Question 1
How are Python dictionaries different from Python lists?
✅ Python lists are indexed using integers and dictionaries can use strings as indexes
❌ Dictionaries store a single value
❌ Dictionaries can only store words
❌ Dictionaries aren’t collections
🧠 Explanation:
Lists use numeric indices, while dictionaries use key–value pairs where keys can be strings or numbers.
Question 2
What is a term commonly used for Python dictionaries in other programming languages?
✅ Associative arrays
❌ Sequences
❌ Lambdas
❌ Closures
🧠 Explanation:
In many languages (like PHP or JavaScript), dictionaries are called associative arrays because they associate keys with values.
Question 3
What would this code print?
stuff = dict()
print(stuff['candy'])
✅ The program would fail with a traceback
❌ -1
❌ 0
❌ candy
🧠 Explanation:
Accessing a missing key directly (like 'candy') causes a KeyError.
Question 4
What would this code print?
stuff = dict()
print(stuff.get('candy', -1))
✅ -1
❌ Program fails
❌ ‘candy’
❌ 0
🧠 Explanation:get() safely looks up a key and returns the default value (-1 here) if the key doesn’t exist.
Question 5
What is a common use of Python dictionaries?
✅ Building a histogram counting the occurrences of various strings in a file
❌ Computing averages
❌ Splitting strings
❌ Sorting names
🧠 Explanation:
Dictionaries are often used as frequency counters (word counts or histograms).
Question 6
Which line is equivalent to this logic?
if key in counts:
counts[key] = counts[key] + 1
else:
counts[key] = 1
✅ counts[key] = counts.get(key, 0) + 1
❌ counts[key] = counts.get(key, -1) + 1
❌ counts[key] = key + 1
❌ counts[key] = (key in counts) + 1
🧠 Explanation:get(key, 0) returns 0 if key doesn’t exist — so you can increment safely.
Question 7
What does this loop iterate through?
x = dict()
for y in x:
...
✅ It loops through the keys in the dictionary
❌ Through the values
❌ Through integers
❌ Through all dictionaries
🧠 Explanation:
By default, looping over a dictionary iterates over its keys.
Question 8
Which method gives you all values in a dictionary?
✅ values()
❌ keys()
❌ items()
❌ all()
🧠 Explanation:
-
.keys()→ list of keys -
.values()→ list of values -
.items()→ list of(key, value)pairs
Question 9
Purpose of the second parameter in .get()?
✅ To provide a default value if the key is not found
❌ Alternate key
❌ Value to retrieve
❌ Key to retrieve
🧠 Explanation:my_dict.get(key, default) prevents a crash by returning default if key doesn’t exist.
🧾 Summary Table
| Q# | ✅ Correct Answer | Key Concept |
|---|---|---|
| 1 | Lists use int indexes; dicts use string keys | Indexing difference |
| 2 | Associative arrays | Dicts in other languages |
| 3 | Traceback error | Missing key access |
| 4 | -1 | .get() with default |
| 5 | Building a histogram | Common dict use |
| 6 | counts[key] = counts.get(key, 0) + 1 | Increment safely |
| 7 | Loops through keys | Iteration behavior |
| 8 | values() | Get all values |
| 9 | Default value if missing | .get() second parameter |