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?

  1. set()
  2. {}
  3. []
  4. ()

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. [1, 2]
  2. (1, 2)
  3. {1, 2}
  4. {"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])
  1. 3 a
  2. 2 c
  3. 1 a
  4. 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

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

Other subjects

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