Dictionaries & Sets questions and answers
Lookups, iteration, hashable keys and set operations. Practise dictionaries & sets questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.
Sample dictionaries & sets questions with worked answers
Question 1
Which expression creates an empty set?
- set()
- {}
- []
- ()
Answer: A. set()
Explanation: Empty braces {} create an empty dictionary, not a set, so the only way to write an empty set is set(). [] is an empty list and () is an empty tuple.
Question 2
Which of the following values can be used as a dictionary key?
- [1, 2]
- (1, 2)
- {1, 2}
- {"a": 1}
Answer: B. (1, 2)
Explanation: A key must be hashable. A tuple of immutable values is hashable, while lists, sets and dictionaries are mutable and cannot be hashed.
Question 3
An int, a float and a bool are used as keys in one dictionary literal. What does this print?
d = {1: "a", 1.0: "b", True: "c"}
print(len(d), d[1])
- 3 a
- 2 c
- 1 a
- 1 c
Answer: D. 1 c
Explanation: 1, 1.0 and True are equal and have the same hash, so they count as the same key. The dictionary ends up with one entry; the first key 1 is kept and its value is overwritten twice, ending as "c".
All 20 dictionaries & sets questions with answers
- A stationery shop updates its stock dictionary. What is printed?
- get() is called for a key that is not in the dictionary, with and without a…
- The same key is written twice in a dictionary literal. What is printed?
- Which expression creates an empty set?
- A price is looked up with square brackets for an item that is not on the menu…
- The union and the intersection of two sets are measured. What does this print?
- From Python 3.7 onwards, in what order does a for loop go through the keys of…
- A key is added and an existing key is updated before the keys are listed. What…
- setdefault() is called twice with the same key. What does the dictionary look…
- A list is used as a dictionary key. What happens when this runs?
- Difference and symmetric difference of two sets are printed in sorted form…
- A dictionary comprehension maps numbers to their squares. What is printed?
- A loop over items() adds up only the marks above 70. What total is printed?
- Which of the following values can be used as a dictionary key?
- pop() is called for a key that exists and for one that does not. What does…
- An int, a float and a bool are used as keys in one dictionary literal. What…
- Sets are built from the letters of two words. What is printed?
- New keys are added to a dictionary inside a for loop over the same dictionary…
- A keys view is taken before the dictionary is changed. What does this print?
- A grade dictionary is inverted with a comprehension. What is printed?
Dictionaries & Sets: common questions
What are dictionaries & sets questions?
Lookups, iteration, hashable keys and set operations. They appear in the python section of many placement tests.
Where can I practise dictionaries & sets questions with answers for free?
Right here on LogicWiz Exams. Every dictionaries & sets question has a worked explanation after you answer. Practice is free and needs no sign-up.
How should I practise dictionaries & sets?
Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.
Start practising
On this page you can start a fresh set of dictionaries & sets questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.
More python topics
- Variables, Types & Operators: Dynamic typing, numbers, operators, precedence and truthiness.
- Python Strings: Indexing, slicing, methods, formatting and immutability.
- Lists & Tuples: Slicing, methods, copies, sorting and unpacking.
- Conditions & Loops: if/elif, for, while, range, break, continue and loop else.
- Functions & Scope: Arguments, defaults, *args, closures, lambda and recursion.
- Comprehensions & Generators: List, dict and set comprehensions, yield and lazy evaluation.
- Classes & Objects: Attributes, inheritance, super(), dunder methods and properties.
- Exceptions & Files: try/except/else/finally, raising errors and the with statement.
- Built-ins & Standard Library: sorted, zip, any/all, collections, itertools and imports.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- SQL: SQL quizzes by topic, from SELECT and joins to window functions: query output and concepts for analyst and developer roles.
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep