Dictionaries & Sets · Question 6 · Easy

The union and the intersection of two sets are measured. What does this print?

a = {1, 2, 3}
b = {3, 4}
print(len(a | b), len(a & b))
  1. 5 1
  2. 4 1
  3. 4 2
  4. 5 0
Answer and explanation

Answer: B. 4 1

Explanation: a | b is the union {1, 2, 3, 4}; the common element 3 is counted once, so the length is 4. a & b is the intersection {3}, with length 1.

Previous: A price is looked up with square brackets for an item that is not on the menu… · Next: From Python 3.7 onwards, in what order does a for loop go through the keys of…

Practise dictionaries & sets

Try a fresh set of dictionaries & sets questions with an explanation after every answer, free and without signing up: Dictionaries & Sets practice. Or sit a full placement mock test.

More dictionaries & sets questions

More python practice

Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge