Sets in Python are an unordered collection of unique elements. They are mutable, allowing the addition and removal of elements, but the elements themselves must be immutable. Sets are useful for membership testing, removing duplicates from a sequence, and performing mathematical operations such as unions, intersections, differences, and symmetric differences. Here we present 15 multiple-choice questions to test your knowledge of Python Sets. Each MCQ has the correct answer with an explanation.
1. What is a set in Python?
Answer:
Explanation:
A set in Python is an unordered collection of unique elements, meaning it does not maintain any order and does not allow duplicates.
2. How do you create an empty set in Python?
Answer:
Explanation:
An empty set is created using the set() function. {} creates an empty dictionary, not a set.
3. Which of the following is a valid set in Python?
Answer:
Explanation:
Sets can contain immutable elements like tuples but cannot contain mutable elements like lists. Sets automatically remove duplicate elements.
4. How do you add an item '4' to a set named 'my_set'?
Answer:
Explanation:
The add() method is used to add a single element to a set.
5. How do you remove an item '3' from a set 'my_set' if you're unsure it exists?
Answer:
Explanation:
discard() removes an element if it exists in the set, but does not raise an error if it doesn't, unlike remove().
6. How can you loop through a set 'my_set' to print all its elements?
Answer:
Explanation:
Looping directly through the set using a for loop is the correct method to access each element.
7. How do you create a union of two sets 'set1' and 'set2'?
Answer:
Explanation:
The union() method returns a new set with all elements from both sets.
8. What does the set method intersection() do?
Answer:
Explanation:
intersection() returns a set containing only elements that are common to both sets.
9. How do you check if 'my_set' is a superset of another set 'another_set'?
Answer:
Explanation:
The method issuperset() checks if the set has every element of the other set.
10. How do you remove all items from a set 'my_set'?
Answer:
Explanation:
The clear() method removes all items from a set, leaving it empty.
11. Which method finds the difference between two sets?
Answer:
Explanation:
difference() returns a set containing elements that are in the first set but not in the second.
12. What does set.pop() do?
Answer:
Explanation:
pop() removes and returns a random element from the set. Sets are unordered, so there is no 'first' or 'last' element.
13. How do you check if two sets 'set1' and 'set2' have no elements in common?
Answer:
Explanation:
isdisjoint() checks if two sets have no elements in common. An empty intersection also indicates no common elements.
14. What is the output of len({1, 2, 3, 4, 4})?
Answer:
Explanation:
Sets do not contain duplicate elements. The set {1, 2, 3, 4, 4} actually is {1, 2, 3, 4}.
15. What is the result of {1, 2, 3} – {2, 3, 4}?
Answer:
Explanation:
The – operator returns the difference of two sets, i.e., elements present in the first set but not in the second.