Python Sets MCQs – Multiple Choice Questions for Practice and Learning
Test and improve your understanding of Python sets with these multiple choice questions. Practice set operations, membership, and manipulation with beginner-friendly MCQs and detailed answers. Ideal for students and Python learners.
Topic: sets
📝 Multiple Choice Questions
🟢 Beginner
Q1. Which of the following is a correct way to create a set in Python?
🟢 A. myset = {}
🔵 B. myset = []
🟠 C. myset = set()
🔴 D. myset = dict()
Answer
myset = set()
Using set() creates an empty set. Using {} creates an empty dictionary.
Q2. What is the output of the following code?
myset = {1, 2, 2, 3}
print(len(myset))
🟢 A. 2
🔵 B. 3
🟠 C. 4
🔴 D. 1
Answer
3
Sets store only unique elements, so duplicate 2 is ignored.
Q3. Which method is used to add an item to a set?
🟢 A. append()
🔵 B. add()
🟠 C. insert()
🔴 D. extend()
Answer
add()
The add() method adds a single element to a set.
Q4. What will be the output?
a = {1, 2, 3}
a.add(2)
print(a)
🟢 A. {1, 2, 3, 2}
🔵 B. {1, 2, 3}
🟠 C. {1, 3}
🔴 D. {1, 2}
Answer
{1, 2, 3}
Adding an existing element has no effect because sets contain only unique items.
Q5. Which method removes an element from a set without raising an error if the element does not exist?
🟢 A. remove()
🔵 B. discard()
🟠 C. pop()
🔴 D. clear()
Answer
discard()
The discard() method does not raise an error if the item is not present.
Q6. Which of these is true about sets?
🟢 A. Sets are ordered collections
🔵 B. Sets can contain duplicate elements
🟠 C. Sets are mutable
🔴 D. Sets can contain lists
Answer
Sets are mutable
You can add and remove elements, but elements must be immutable.
Q7. Which operator is used for set union?
🟢 A. +
🔵 B. &
🟠 C. |
🔴 D. -
Answer
|
The '|' operator returns the union of two sets.
Q8. What is the output?
a = {1, 2}
b = {2, 3}
print(a | b)
🟢 A. {1, 2, 3}
🔵 B. {2}
🟠 C. {1, 3}
🔴 D. {1, 2}
Answer
{1, 2, 3}
Union combines elements from both sets, removing duplicates.
Q9. Which method removes all items from a set?
🟢 A. delete()
🔵 B. remove()
🟠 C. clear()
🔴 D. discard()
Answer
clear()
clear() empties the set completely.
Q10. Which method can be used to get a copy of a set?
🟢 A. copy()
🔵 B. clone()
🟠 C. duplicate()
🔴 D. replicate()
Answer
copy()
copy() returns a shallow copy of the set.
Q11. Which set operation returns elements common to both sets?
🟢 A. Union
🔵 B. Difference
🟠 C. Intersection
🔴 D. Symmetric Difference
Answer
Intersection
Intersection returns common elements.
Q12. What is the output?
a = {1, 2, 3}
b = {3, 4, 5}
print(a & b)
🟢 A. {1, 2, 3, 4, 5}
🔵 B. {3}
🟠 C. {1, 2}
🔴 D. {}
Answer
{3}
The '&' operator returns the intersection of the sets.
Q13. Which operation returns elements that are in either set but not both?
🟢 A. Union
🔵 B. Intersection
🟠 C. Difference
🔴 D. Symmetric Difference
Answer
Symmetric Difference
Symmetric difference excludes common elements.
Q14. What does the pop() method do on a set?
🟢 A. Removes and returns an arbitrary element
🔵 B. Removes a specific element
🟠 C. Removes all elements
🔴 D. Adds an element
Answer
Removes and returns an arbitrary element
Sets are unordered; pop() removes any item.
Q15. What is the output?
myset = {1, 2, 3}
myset.remove(4)
🟢 A. Removes nothing
🔵 B. Raises KeyError
🟠 C. Removes 4
🔴 D. Adds 4
Answer
Raises KeyError
remove() raises an error if the element is not found.
Q16. What is the result of set([1, 2, 2, 3])?
🟢 A. [1, 2, 2, 3]
🔵 B. {1, 2, 2, 3}
🟠 C. {1, 2, 3}
🔴 D. (1, 2, 3)
Answer
{1, 2, 3}
The set() constructor removes duplicates.
Q17. Which set method returns the difference of two sets?
🟢 A. intersection()
🔵 B. difference()
🟠 C. union()
🔴 D. symmetric_difference()
Answer
difference()
difference() returns elements only in the first set.
Q18. What is the output?
a = {1, 2, 3}
b = {2, 3, 4}
print(a - b)
🟢 A. {1, 4}
🔵 B. {2, 3}
🟠 C. {1}
🔴 D. {}
Answer
{1}
Difference returns items in a but not in b.
Q19. What is the data type of { } in Python?
🟢 A. Set
🔵 B. List
🟠 C. Tuple
🔴 D. Dictionary
Answer
Dictionary
Empty {} is a dictionary. Use set() for an empty set.
Q20. Which is a valid way to check if an item exists in a set?