Python set MCQ questions with answers
Unique values and set operations, with the questions that trip people up.
0 of 20 answered
Question 1 · Easy
What is the output of this code?
nums = [3, 1, 3, 2, 1, 3] unique = set(nums) print(len(unique), sorted(unique))
Show the answer
Answer: A
A set keeps one copy of each value, so the six items shrink to three.
Run this code in the editorsorted()returns those values as a sorted list.Question 2 · Easy
What is the output of this code?
letters = set('hello') print(len(letters))
Show the answer
Answer: B
Run this code in the editorset()takes each character of the string, and the two'l'characters count once, leaving h, e, l and o. For a set holding the whole word, write{'hello'}.Question 3 · Easy
What is the output of this code?
a = {1, 2, 3, 4} b = {3, 4, 5} print(sorted(a | b), sorted(a & b))
Show the answer
Answer: C
Run this code in the editor|is union: every value that is in either set, each once.&is intersection: only the values that are in both sets, here 3 and 4.Question 4 · Easy
What is the output of this code?
s = {10, 20} s.add(20) s.add(30) print(len(s))
Show the answer
Answer: D
Adding a value that is already in a set changes nothing, so
Run this code in the editoradd(20)has no effect. Only 30 is new, which gives three items.Question 5 · Easy
What is the output of this code?
s = {1, 2, 3} s.discard(5) s.discard(2) print(sorted(s))
Show the answer
Answer: B
Run this code in the editordiscard()removes a value if it is present and does nothing otherwise, sodiscard(5)raises no error anddiscard(2)removes 2.remove(5)would have raisedKeyError.Question 6 · Easy
Which error does this code raise?
s = {1, 2, 3} s.remove(4)
Show the answer
Answer: A, KeyError: 4
Run this code in the editorremove()on a set raisesKeyErrorwhen the value is missing. A list raisesValueErrorin the same situation, which makes it the tempting wrong answer. Usediscard()to remove without an error.Question 7 · Easy
Which statement about a Python
setis true?Show the answer
Answer: D, It stores each value once and has no index positions
A set holds unique values with no defined order and no indexing. It is mutable (
add(),remove()), but its items must be hashable, so lists cannot go in.frozensetis the immutable version.Question 8 · Easy
Which expression gives the number of different values in
nums = [4, 2, 4, 4, 2, 9]?Show the answer
Answer: A
set(nums)keeps one copy of each value andlen()counts them, giving 3.{nums}tries to put the whole list inside a set and raisesTypeError, because lists are not hashable.Question 9 · Medium
What is the output of this code?
a = {1, 2, 3, 4} b = {3, 4, 5, 6} print(sorted(a - b), sorted(a ^ b))
Show the answer
Answer: A
Run this code in the editora - bkeeps the values ofathat are not inb.a ^ bis the symmetric difference: the values that are in exactly one of the two sets, so the shared 3 and 4 drop out.Question 10 · Medium
What is the output of this code?
s = set() s.add('ab') s.update('ab') print(len(s), sorted(s))
Show the answer
Answer: D
Run this code in the editoradd()inserts its argument as one item, the string'ab'.update()treats its argument as an iterable and adds each character,'a'and'b'. The set ends with three strings, whichsorted()lists alphabetically.Question 11 · Medium
What is the output of this code?
a = {1, 2} b = {1, 2, 3} print(a <= b, a < a, b >= a)
Show the answer
Answer: B
Run this code in the editor<=tests for a subset and>=for a superset, so the first and last are True.<tests for a proper subset, which also requires the sets to differ, soa < ais False.Question 12 · Medium
What is the output of this code?
a = {1, 2, 3} result = a.intersection_update({2, 3, 4}) print(result, sorted(a))
Show the answer
Answer: C
Methods whose names end in
Run this code in the editor_updatechange the set in place and returnNone, likelist.sort().akeeps only the values that are also in the other set.a.intersection(...)would return a new set instead.Question 13 · Medium
What is the output of this code?
pairs = {frozenset({1, 2}), frozenset({2, 1}), frozenset({3})} print(len(pairs))
Show the answer
Answer: C
A
Run this code in the editorfrozensetis an immutable set, so it is hashable and can be an item of another set. Sets ignore order, sofrozenset({1, 2})andfrozenset({2, 1})are equal and count once.Question 14 · Medium
Which error does this code raise?
colors = {'red', 'green', 'blue'} print(colors[0])
Show the answer
Answer: B, TypeError: 'set' object is not subscriptable
Sets have no positions, so they do not support indexing at all and raise
Run this code in the editorTypeError. Convert first, for examplesorted(colors)[0], when you need an item by position.Question 15 · Medium
How does a
frozensetdiffer from aset?Show the answer
Answer: A, A frozenset cannot be changed, so it is hashable and can be a dictionary key
A frozenset supports union, intersection and subset tests like a set but has no methods that change it. Because it is immutable it is hashable, so it can be a dictionary key or an item of another set.
Question 16 · Medium
What does the expression
{}create?Show the answer
Answer: B, An empty dictionary
Empty braces mean an empty dictionary, so an empty set must be written
set(). Braces with items and no colons, such as{1, 2}, do make a set.Question 17 · Hard
What is the output of this code?
s = {1, 1.0, True, 'True'} print(len(s))
Show the answer
Answer: D
Run this code in the editor1,1.0andTrueare equal and have the same hash, so the set stores only one of them. The string'True'is a different value, so the set has two items.Question 18 · Hard
What is the output of this code?
a = {1, 2} b = a a |= {3} a = a | {4} print(sorted(b))
Show the answer
Answer: D
Run this code in the editora |= {3}updates the set in place, sob, which names the same set, gains 3.a = a | {4}builds a new set and rebinds onlya;bstill holds{1, 2, 3}.Question 19 · Hard
Which error does this code raise?
evens = {2, 4} evens = evens.union([6]) evens = evens | [8]
Show the answer
Answer: C, TypeError: unsupported operand type(s) for |: 'set' and 'list'
Set methods such as
Run this code in the editorunion()accept any iterable, so the second line works. The|operator requires a set on both sides, soevens | [8]raisesTypeError. Writeevens | {8}instead.Question 20 · Hard
Which expression removes duplicates from
namesand keeps the order in which the names first appear?Show the answer
Answer: C
Dictionary keys are unique and keep insertion order, so
dict.fromkeys()drops repeats in first-seen order. A set also drops repeats but has no defined order,sorted()gives alphabetical order, and thecount()filter removes repeated names entirely.
Keep going
- Read the lesson behind these questions: Sets in the PythonBook
- Next topic: Python list comprehension MCQ questions with answers
- Before this: Python dictionary MCQ questions with answers
- Write the code yourself in the Python coding challenges, which check your solution with tests.