Python Sets Quiz
A 35-question quiz covering set creation, uniqueness, adding/removing items, and mathematical operations like union and intersection.
Question 1
Which of the following commands correctly creates an empty set in Python?
Question 2
Analyze the code below. What will be the final content of the variable `unique_numbers`?
numbers = [1, 2, 2, 3, 3, 3]
unique_numbers = set(numbers)Question 3
What is the primary difference between the `remove()` and `discard()` methods when deleting an item from a set?
Question 4
Consider the following two sets. Which expression correctly calculates their intersection (elements present in both)?
set_a = {1, 2, 3}
set_b = {3, 4, 5}Question 5
What happens when you try to add a mutable object, such as a list, to a set?
s = {1, 2}
s.add([3, 4])Question 6
Which method would you use to add a single element to an existing set?
Question 7
Analyze the code below. What is the output of the print statement?
s = {1, 2, 3}
print(4 in s)Question 8
What is the result of the union operation between `s1 = {1, 2}` and `s2 = {2, 3}`?
Question 9
Why are sets considered 'unordered' collections?
Question 10
Which of the following code snippets correctly creates a set with initial values?
Question 11
What is the output of `len({1, 2, 3, 3, 2, 1})`?
Question 12
Which operator is used to calculate the symmetric difference (elements in either set, but not both)?
Question 13
Analyze the code below. What happens when `pop()` is called on the set?
fruits = {"apple", "banana", "cherry"}
item = fruits.pop()Question 14
What is the result of subtracting set B from set A: `A - B`, where `A = {1, 2, 3}` and `B = {3, 4}`?
Question 15
How can you remove all elements from a set at once?
Question 16
Which of the following is true about the time complexity of checking membership (`x in s`) in a set?
Question 17
What is the output of the following code involving the `update()` method?
s = {1, 2}
s.update([2, 3, 4])Question 18
Can a set contain another set as an element?
s = {1, {2, 3}}Question 19
What does the `isdisjoint()` method check?
Question 20
Which Python function creates an immutable version of a set?
Question 21
Analyze the code below. What is the value of `result`?
a = {1, 2, 3}
b = {1, 2}
result = b.issubset(a)Question 22
What happens if you iterate over a set using a `for` loop?
Question 23
Which of the following is NOT a valid way to remove an item from a set?
Question 24
What is the result of `set('hello')`?
Question 25
How do you check if set A is a proper subset of set B (A is in B, but A != B)?
Question 26
What is the output of the following set comprehension?
squares = {x*x for x in range(4)}Question 27
Can you use the index operator `[]` to access elements in a set?
s = {1, 2, 3}
print(s[0])Question 28
What happens if you try to create a set with duplicate values explicitly?
s = {1, 1, 1}Question 29
Which of the following data types is hashable and can be added to a set?
Question 30
What is the result of `s = {1, 2}; s.add(2)`?
Question 31
Which method returns a copy of the set?
Question 32
What is the output of the following code?
a = {1, 2}
b = {2, 1}
print(a == b)Question 33
Which operation creates a set containing elements that are in A or B, but not in both?
Question 34
What is the correct syntax to check if `x` is NOT in set `s`?
Question 35
Analyze the code below. What is the final size of the set?
s = set()
s.add(1)
s.add(1.0)
s.add(True)