The program below stops with an error. Which statement correctly describes the problem?
prices = {"tea": 20, "coffee": 35}
print("Juice costs", prices["juice"])
[] demands that the key exists; get() lets you supply a default.
Dictionaries · Exercise 3 of 4
Each Python program has a dictionary mistake: a syntax error, an exception at run time, or a logical bug such as searching values with in. Choose the option that names the real problem.
The program below stops with an error. Which statement correctly describes the problem?
prices = {"tea": 20, "coffee": 35}
print("Juice costs", prices["juice"])
[] demands that the key exists; get() lets you supply a default.
The program below stops with an error. Which statement correctly describes the problem?
seats = {}
seats[[3, 7]] = "booked"
print(seats)
Composite keys are written as tuples: seats[(3, 7)] or simply seats[3, 7].
The program below stops with an error. Which statement correctly describes the problem?
phone_book = {"Asha": "98450"}
phone_book.append({"Ravi": "99001"})
print(phone_book)
Assign to a key, or merge with update() / |=.
The program below stops with an error. Which statement correctly describes the problem?
codes = dict(["IN", "US", "JPN"])
print(codes)
Each item must unpack into exactly a key and a value. A 2-character string does, which is why the first two silently become {"I": "N", "U": "S"} – a hidden bug in itself.
The program below stops with an error. Which statement correctly describes the problem?
cart = {"book": 450, "pen": 20}
print("Total:", sum(cart))
Anything that iterates a dict sees the keys unless you ask for values() or items().
Someone did score 91, but the program says nobody did. Which statement correctly identifies the bug?
marks = {"Asha": 91, "Ravi": 78}
if 91 in marks:
print("Someone scored 91")
else:
print("Nobody scored 91")
Membership, iteration and len() all work on keys by default.
Only Asha's list should contain 91. Which statement correctly identifies the bug?
marks = dict.fromkeys(["Asha", "Ravi", "Meera"], [])
marks["Asha"].append(91)
print(marks)
The default value is created once and shared.
"the" appears three times, but the program counts it once. Which statement correctly identifies the bug?
text = "the cat and the hat and the bat"
counts = {}
for w in text.split():
counts[w] = 1
print(counts["the"], counts["and"])
Assigning a constant resets the value. Use get() with a default, defaultdict(int) or Counter.
Python refuses to run the program below. Which statement correctly describes the problem?
capitals = {"India": "Delhi", "Japan" "Tokyo"}
print(capitals)
Adjacent strings would merge into "JapanTokyo", which then leaves a lone item without a key: value form inside a dict display – Python rejects it.
Only the draft settings should change. Which statement correctly identifies the bug?
settings = {"theme": "light", "font": {"size": 14}}
draft = settings.copy()
draft["font"]["size"] = 18
print(settings["font"]["size"])
A shallow copy duplicates only the outer dict; values are the same objects.
Timed online tests with instant scores and explanations.