Sets in python: 18 MCQs with answers
All 18 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 8 questions
Q1. What is the correct way to create an empty set in Python?
BeginnerShow answer & explanation
set()
Using `set()` creates an empty set, while `{}` creates an empty dictionary.
Q2. Which of the following is a property of a Python set?
BeginnerShow answer & explanation
Mutable
Sets are mutable (can add/remove elements) but unordered and do not allow duplicates.
Q3. Which method adds an element to a set?
BeginnerShow answer & explanation
add()
`add()` is the correct method for sets; `append()` and `extend()` are for lists.
Q4. What is the output of `len({1, 2, 3, 2, 1})`?
BeginnerShow answer & explanation
3
Sets automatically remove duplicates, so the set becomes {1, 2, 3} with length 3.
Q5. A set can contain duplicate elements.
BeginnerShow answer & explanation
False
Sets are collections of unique elements; duplicates are automatically removed.
Q6. You can access elements of a set using an index like `my_set[0]`.
BeginnerShow answer & explanation
False
Sets are unordered, so they do not support indexing or slicing.
Q7. The `clear()` method removes all elements from a set.
BeginnerShow answer & explanation
True
`clear()` empties the set, leaving it with no elements.
Q8. A set can contain both integers and strings.
BeginnerShow answer & explanation
True
Sets can hold heterogeneous hashable types, including integers and strings.
Building up · 9 questions
Q9. What will be the output of `{1, 2, 3} | {2, 3, 4}`?
IntermediateShow answer & explanation
{1, 2, 3, 4}
The `|` operator performs union, combining all unique elements from both sets.
Q10. What does `{1, 2, 3} - {2, 3, 4}` return?
IntermediateShow answer & explanation
{1}
The `-` operator performs difference, returning elements in the first set not in the second.
Q11. Which of the following cannot be an element of a set?
IntermediateShow answer & explanation
List
Sets require hashable elements; lists are mutable and unhashable, so they cannot be added.
Q12. Which operator checks if a set is a subset of another?
IntermediateShow answer & explanation
<=
`
Q13. What does `{1, 2, 3} & {2, 3, 4}` return?
IntermediateShow answer & explanation
{2, 3}
The `&` operator performs intersection, returning elements common to both sets.
Q14. Which method removes an element from a set and raises an error if not found?
IntermediateShow answer & explanation
remove()
`remove()` removes the element and raises KeyError if missing; `discard()` does not raise an error.
Q15. What is the output of `{x**2 for x in range(3)}`?
IntermediateShow answer & explanation
{0, 1, 4}
This is a set comprehension; range(3) gives 0,1,2 and squares are 0,1,4.
Q16. What does `{1, 2}.issuperset({1})` return?
IntermediateShow answer & explanation
True
`issuperset()` returns True if the set contains all elements of the argument set.
Q17. The `^` operator performs symmetric difference on sets.
IntermediateShow answer & explanation
True
Symmetric difference returns elements in either set but not both.
Topper level · 1 questions
Q18. Which of the following is true about frozenset?
AdvancedShow answer & explanation
It can be used as a dictionary key
A frozenset is immutable and hashable, so it can be used as a dictionary key.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages