LearnFrenzy
Sign in Take a free test

Dictionaries · Exercise 2 of 4

Dictionaries Predict the Output

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

Read each Python program and choose what it prints. Every program was run on Python 3, so the answers are the real output.

Question 1 Easy

What is the output of the following Python program?

main.py
d = {"a": 1, "b": 2}
d["c"] = 3
d["a"] = 10
print(d, len(d))
A {'b': 2, 'c': 3, 'a': 10} 3
B {'a': 10, 'b': 2, 'c': 3} 3
C {'a': 1, 'b': 2, 'c': 3, 'a': 10} 4
D {'a': 10, 'b': 2, 'c': 3} 4
Question page & discussion
Question 2 Easy

What is the output of the following Python program?

main.py
d = {"a": 10}
print(d.get("z"), d.get("z", 0), "a" in d, 10 in d, 10 in d.values())
A KeyError
B None 0 True True True
C 0 0 True False True
D None 0 True False True
Question page & discussion
Question 3 Medium

What is the output of the following Python program?

main.py
d = {"x": 1, "y": 2, "x": 3}
print(d, len(d))
A {'x': 3, 'y': 2} 2
B {'x': 1, 'y': 2} 2
C {'y': 2, 'x': 3} 2
D {'x': 1, 'y': 2, 'x': 3} 3
Question page & discussion
Question 4 Hard

What is the output of the following Python program?

main.py
d = {1: "int", True: "bool", 1.0: "float"}
print(d, len(d))
A {1: 'float'} 1
B {1: 'int', True: 'bool', 1.0: 'float'} 3
C {True: 'bool'} 1
D {1.0: 'float'} 1
Question page & discussion
Question 5 Medium

What is the output of the following Python program?

main.py
d = {"a": 1, "b": 2, "c": 3}
print(d.pop("b"), d.popitem(), d, d.pop("z", "none"))
A 2 ('a', 1) {'c': 3} none
B 2 ('c', 3) {'a': 1} KeyError
C ('b', 2) ('c', 3) {'a': 1} none
D 2 ('c', 3) {'a': 1} none
Question page & discussion
Question 6 Medium

What is the output of the following Python program?

main.py
d = {"a": 1}
k = d.keys()
snapshot = list(d.keys())
d["b"] = 2
print(k, snapshot, list(d.values()))
A dict_keys(['a']) ['a'] [1, 2]
B ['a', 'b'] ['a', 'b'] [1, 2]
C dict_keys(['a', 'b']) ['a'] [1, 2]
D dict_keys(['a', 'b']) ['a', 'b'] [1, 2]
Question page & discussion
Question 7 Hard

What is the output of the following Python program?

main.py
d = {}
d.setdefault("x", []).append(1)
d.setdefault("x", []).append(2)
print(d, d.setdefault("y", 0))
A {'x': [1, 2]} 0
B {'x': [2]} 0
C {'x': [1, 2], 'y': 0} 0
D {'x': [1, 2]} None
Question page & discussion
Question 8 Easy

What is the output of the following Python program?

main.py
counts = {}
for ch in "banana":
    counts[ch] = counts.get(ch, 0) + 1
print(counts)
A {'a': 3, 'b': 1, 'n': 2}
B {'b': 1, 'a': 1, 'n': 1}
C KeyError
D {'b': 1, 'a': 3, 'n': 2}
Question page & discussion
Question 9 Medium

What is the output of the following Python program?

main.py
a = {"x": 1, "y": 2}
b = {"y": 20, "z": 30}
print(a | b, {**b, **a})
A {'x': 1, 'y': 2, 'z': 30} {'y': 20, 'z': 30, 'x': 1}
B {'x': 1, 'y': 20, 'z': 30} {'x': 1, 'y': 2, 'z': 30}
C {'x': 1, 'y': 20, 'z': 30} {'y': 2, 'z': 30, 'x': 1}
D {'x': 1, 'y': [2, 20], 'z': 30} {'y': [20, 2], 'z': 30, 'x': 1}
Question page & discussion
Question 10 Medium

What is the output of the following Python program?

main.py
print({n: n * n for n in range(1, 5) if n % 2 == 0}, {v: k for k, v in {"a": 1, "b": 2}.items()})
A {2: 4, 4: 16} {1: 'a', 2: 'b'}
B {1: 1, 3: 9} {1: 'a', 2: 'b'}
C {2: 4, 4: 16} {'a': 1, 'b': 2}
D [4, 16] {1: 'a', 2: 'b'}
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links