LearnFrenzy
Sign in Take a free test

Dictionaries · Exercise 3 of 4

Dictionaries Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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.

Question 1 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
prices = {"tea": 20, "coffee": 35}
print("Juice costs", prices["juice"])
A KeyError: "juice" is not a key; use prices.get("juice") or check "juice" in prices
B It prints Juice costs None
C IndexError
D ValueError
Question page & discussion
Question 2 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
seats = {}
seats[[3, 7]] = "booked"
print(seats)
A TypeError: lists are unhashable and cannot be keys; use the tuple (3, 7)
B KeyError: [3, 7]
C It prints {[3, 7]: 'booked'}
D IndexError
Question page & discussion
Question 3 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
phone_book = {"Asha": "98450"}
phone_book.append({"Ravi": "99001"})
print(phone_book)
A TypeError: dicts cannot be added
B It prints both entries
C KeyError
D AttributeError: dict has no append(); add with phone_book["Ravi"] = "99001" or update()
Question page & discussion
Question 4 Hard

The program below stops with an error. Which statement correctly describes the problem?

main.py
codes = dict(["IN", "US", "JPN"])
print(codes)
A It prints {'I': 'N', 'U': 'S', 'J': 'PN'}
B TypeError: strings are not allowed
C ValueError: dict() needs pairs; "IN" and "US" happen to be 2-character strings, but "JPN" has length 3 – pass (key, value) tuples
D KeyError: JPN
Question page & discussion
Question 5 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
cart = {"book": 450, "pen": 20}
print("Total:", sum(cart))
A TypeError: iterating a dict gives its keys (strings), so sum() tries 0 + "book"; use sum(cart.values())
B It prints Total: 470
C KeyError: book
D ValueError
Question page & discussion
Question 6 Easy

Someone did score 91, but the program says nobody did. Which statement correctly identifies the bug?

main.py
marks = {"Asha": 91, "Ravi": 78}
if 91 in marks:
    print("Someone scored 91")
else:
    print("Nobody scored 91")
A in on a dict checks the keys (names); use 91 in marks.values()
B in only works on lists
C 91 must be a string
D The dict must be sorted first
Question page & discussion
Question 7 Hard

Only Asha's list should contain 91. Which statement correctly identifies the bug?

main.py
marks = dict.fromkeys(["Asha", "Ravi", "Meera"], [])
marks["Asha"].append(91)
print(marks)
A fromkeys() puts the SAME list object under every key; build separate lists with {name: [] for name in names}
B append() adds to every list in a dict
C Lists cannot be dictionary values
D print shows the wrong dict
Question page & discussion
Question 8 Medium

"the" appears three times, but the program counts it once. Which statement correctly identifies the bug?

main.py
text = "the cat and the hat and the bat"
counts = {}
for w in text.split():
    counts[w] = 1
print(counts["the"], counts["and"])
A counts[w] = 1 overwrites the count every time; increment with counts[w] = counts.get(w, 0) + 1
B split() removes duplicate words
C Dict keys must be unique, so repeats are lost
D The loop runs only once
Question page & discussion
Question 9 Easy

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
capitals = {"India": "Delhi", "Japan" "Tokyo"}
print(capitals)
A Dictionaries need square brackets
B The pair "Japan" "Tokyo" has no colon between key and value
C Country names must be lowercase
D There is no error
Question page & discussion
Question 10 Medium

Only the draft settings should change. Which statement correctly identifies the bug?

main.py
settings = {"theme": "light", "font": {"size": 14}}
draft = settings.copy()
draft["font"]["size"] = 18
print(settings["font"]["size"])
A copy() is shallow: the inner "font" dict is shared; use copy.deepcopy(settings)
B copy() returns the same dict
C Nested dicts are read-only
D size must be a string
Question page & discussion
Up next · Exercise 4Dictionaries Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links