Editor

Python set MCQ questions with answers

Unique values and set operations, with the questions that trip people up.

0 of 20 answered

  1. 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. sorted() returns those values as a sorted list.

    Run this code in the editor
  2. Question 2 · Easy

    What is the output of this code?

    letters = set('hello')
    print(len(letters))
    Show the answer

    Answer: B

    set() 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'}.

    Run this code in the editor
  3. 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

    | 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.

    Run this code in the editor
  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 add(20) has no effect. Only 30 is new, which gives three items.

    Run this code in the editor
  5. 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

    discard() removes a value if it is present and does nothing otherwise, so discard(5) raises no error and discard(2) removes 2. remove(5) would have raised KeyError.

    Run this code in the editor
  6. Question 6 · Easy

    Which error does this code raise?

    s = {1, 2, 3}
    s.remove(4)
    Show the answer

    Answer: A, KeyError: 4

    remove() on a set raises KeyError when the value is missing. A list raises ValueError in the same situation, which makes it the tempting wrong answer. Use discard() to remove without an error.

    Run this code in the editor
  7. Question 7 · Easy

    Which statement about a Python set is 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. frozenset is the immutable version.

  8. 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 and len() counts them, giving 3. {nums} tries to put the whole list inside a set and raises TypeError, because lists are not hashable.

  9. 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

    a - b keeps the values of a that are not in b. a ^ b is the symmetric difference: the values that are in exactly one of the two sets, so the shared 3 and 4 drop out.

    Run this code in the editor
  10. 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

    add() 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, which sorted() lists alphabetically.

    Run this code in the editor
  11. 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

    <= 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, so a < a is False.

    Run this code in the editor
  12. 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 _update change the set in place and return None, like list.sort(). a keeps only the values that are also in the other set. a.intersection(...) would return a new set instead.

    Run this code in the editor
  13. 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 frozenset is an immutable set, so it is hashable and can be an item of another set. Sets ignore order, so frozenset({1, 2}) and frozenset({2, 1}) are equal and count once.

    Run this code in the editor
  14. 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 TypeError. Convert first, for example sorted(colors)[0], when you need an item by position.

    Run this code in the editor
  15. Question 15 · Medium

    How does a frozenset differ from a set?

    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.

  16. 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.

  17. Question 17 · Hard

    What is the output of this code?

    s = {1, 1.0, True, 'True'}
    print(len(s))
    Show the answer

    Answer: D

    1, 1.0 and True are 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.

    Run this code in the editor
  18. 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

    a |= {3} updates the set in place, so b, which names the same set, gains 3. a = a | {4} builds a new set and rebinds only a; b still holds {1, 2, 3}.

    Run this code in the editor
  19. 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 union() accept any iterable, so the second line works. The | operator requires a set on both sides, so evens | [8] raises TypeError. Write evens | {8} instead.

    Run this code in the editor
  20. Question 20 · Hard

    Which expression removes duplicates from names and 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 the count() filter removes repeated names entirely.

Keep going