LearnFrenzy
Sign in Take a free test

Dictionaries · Exercise 2 of 4

Dictionaries Predict the Output

  • 15 questions
  • Page 2 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 11 Hard

What is the output of the following Python program?

main.py
d = dict.fromkeys(["a", "b"], [])
d["a"].append(1)
e = {k: [] for k in ["a", "b"]}
e["a"].append(1)
print(d, e)
A {'a': [1], 'b': []} {'a': [1], 'b': []}
B {'a': [1], 'b': [1]} {'a': [1], 'b': [1]}
C TypeError
D {'a': [1], 'b': [1]} {'a': [1], 'b': []}
Question page & discussion
Question 12 Medium

What is the output of the following Python program?

main.py
marks = {"Ravi": 95, "Asha": 91, "Meera": 85}
print(sorted(marks))
print(sorted(marks, key=marks.get, reverse=True))
print(max(marks, key=marks.get), min(marks.values()))
A ['Ravi', 'Asha', 'Meera'] ['Ravi', 'Asha', 'Meera'] Ravi 85
B ['Asha', 'Meera', 'Ravi'] ['Ravi', 'Asha', 'Meera'] Ravi 85
C ['Asha', 'Meera', 'Ravi'] ['Meera', 'Asha', 'Ravi'] Ravi Meera
D [85, 91, 95] [95, 91, 85] 95 85
Question page & discussion
Question 13 Easy

What is the output of the following Python program?

main.py
d = {"a": 1, "b": 2}
d.update(b=5, c=7)
for k, v in d.items():
    print(k, v, end="; ")
print()
A a 1; b 2; c 7;
B a 1; b 2; b 5; c 7;
C b 5; c 7; a 1;
D a 1; b 5; c 7;
Question page & discussion
Question 14 Medium

What is the output of the following Python program?

main.py
emp = {"name": "Asha", "skills": {"python": 5, "sql": 3}}
emp["skills"]["sql"] += 1
emp["skills"]["git"] = 2
print(emp["skills"], len(emp), len(emp["skills"]))
A {'python': 5, 'sql': 3, 'git': 2} 2 3
B {'python': 5, 'sql': 4, 'git': 2} 3 3
C {'python': 5, 'sql': 4, 'git': 2} 2 3
D KeyError
Question page & discussion
Question 15 Medium

What is the output of the following Python program?

main.py
from collections import Counter

c = Counter("mississippi")
print(c.most_common(2), c["z"])
print({"a": 1, "b": 2} == {"b": 2, "a": 1}, list({"b": 1, "a": 2}))
A [('s', 4), ('i', 4)] KeyError True ['a', 'b']
B [('i', 4), ('s', 4)] 0 False ['b', 'a']
C [('i', 4), ('s', 4)] 0 True ['b', 'a']
D [('i', 4), ('s', 4)] None True ['a', 'b']
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links