Python Sets Quiz
Welcome to the Python Sets Quiz!
Sets are unordered collections with no duplicate elements. This quiz will test your knowledge of sets in Python, covering concepts such as set operations, properties, and methods.
Each question has one correct answer. Try your best, and let’s see how much you know about sets!
Table of Contents:
- Python quizzes for beginners series
Sets Quiz
Questions & Answers
Basic Concepts
1. What is a set in Python?
a) A collection of ordered items
b) A collection of unique and unordered items
c) A mutable list
d) A tuple with unique values
Answer
b) A collection of unique and unordered items
2. Which of the following is the correct way to create an empty set?
a) set = {}
b) set = set()
c) set = []
d) set = {None}
Answer
b) set = set()
3. What will be the output of {1, 2, 2, 3, 4, 4, 5}
?
a) {1, 2, 2, 3, 4, 4, 5}
b) {1, 2, 3, 4, 5}
c) [1, 2, 3, 4, 5]
d) (1, 2, 3, 4, 5)
Answer
b) {1, 2, 3, 4, 5}
4. Which of the following is NOT a property of sets?
a) Sets are unordered
b) Sets allow duplicate elements
c) Sets are mutable
d) Sets do not support indexing
Answer
b) Sets allow duplicate elements
5. Which data types can be stored in a Python set?
a) Only integers
b) Only strings
c) Any immutable data type
d) Any data type, including lists
Answer
c) Any immutable data type
Set Operations
6. What does the .add()
method do in a set?
a) Adds multiple elements
b) Adds a single element
c) Adds elements in sorted order
d) Adds an element at a specific index
Answer
b) Adds a single element
7. Which operator is used for set union?
a) &
b) |
c) -
d) ^
Answer
b) |
8. What will be the output of {1, 2, 3} | {3, 4, 5}
?
a) {1, 2, 3, 4, 5}
b) {3}
c) {}
d) {1, 2, 3, 3, 4, 5}
Answer
a) {1, 2, 3, 4, 5}
9. What is the result of {1, 2, 3} & {2, 3, 4}
?
a) {1, 2, 3, 4}
b) {2, 3}
c) {}
d) {1}
Answer
b) {2, 3}
10. What is the result of {1, 2, 3} - {2, 3, 4}
?
a) {1}
b) {}
c) {2, 3}
d) {4}
Answer
a) {1}
Set Methods
- What does the
.update()
method do?
a) Removes duplicates from a set
b) Adds multiple elements to a set
c) Removes an element
d) Clears the set
Answer
b) Adds multiple elements to a set
- Which method is used to remove an element from a set safely (without an error if the element is missing)?
a).discard()
b).remove()
c).delete()
d).pop()
Answer
a) .discard()
- What does the
.pop()
method do in a set?
a) Removes the last element
b) Removes the first element
c) Removes and returns a random element
d) Clears the set
Answer
c) Removes and returns a random element
- How do you check if an element exists in a set?
a)set.find(element)
b)set[element]
c)element in set
d)set.has(element)
Answer
c) element in set
- What does
set.clear()
do?
a) Deletes one element from the set
b) Deletes the entire set
c) Removes all elements but keeps the set empty
d) Returns a new set
Answer
c) Removes all elements but keeps the set empty
Advanced Set Concepts
- What is the difference between
.remove()
and.discard()
?
a) No difference
b).remove()
raises an error if the element is missing, but.discard()
does not
c).remove()
deletes all occurrences, but.discard()
deletes only one
d).remove()
works on lists, but.discard()
works on sets
Answer
b) .remove()
raises an error if the element is missing, but .discard()
does not
- What does the
.symmetric_difference()
method do?
a) Returns the common elements
b) Returns the elements unique to each set
c) Removes duplicates from a set
d) Sorts the set
Answer
b) Returns the elements unique to each set
- Which method can be used to check if one set is a subset of another?
a).issubset()
b).issuperset()
c).ispartof()
d).subsetof()
Answer
a) .issubset()
- What will
A = {1, 2, 3}; B = {1, 2, 3, 4, 5}; print(A.issubset(B))
return?
a)True
b)False
c)Error
d){1, 2, 3}
Answer
a) True
- What will
A = {1, 2, 3, 4, 5}; B = {1, 2, 3}; print(A.issuperset(B))
return?
a)True
b)False
c)Error
d){1, 2, 3, 4, 5}
Answer
a) True
How did you do? 🎯
- 18-20 correct → 🏆 Excellent! You’re a Python functions pro!
- 14-17 correct → 👍 Great job! Keep practicing.
- 10-13 correct → 🙂 Good, but there’s room for improvement.
- Below 10 → 🤔 No worries! Review the concepts and try again.