Python Set Quiz – 30 MCQ for Coding Practice
Welcome to this ultimate Python Set quiz! Here, we have collected 30 questions based on set in Python, which will cover basic to advanced concepts. This MCQ quiz will test your knowledge in set as well as level up your understanding in set.
Whether you’re a beginner or an experienced Python programmer, this quiz is perfect for coding interviews, practices, and enhancing your knowledge in Python set. So, are you ready to score 30/30? Let’s see, how well do you really understand Python set? Let’s begin with question number 1.👇
1. What is a set in Python?
A. A collection of ordered elements.
B. A collection of elements in key-value pairs.
C. A collection of unordered elements.
D. An unordered collection of unique elements or values.
d
A set in Python is another data type that contains an unordered collection of unique elements or values. For example, suppose we want to store information about student roll nos. Since student roll nos cannot be duplicate, we can use a set.
2. Which of the following is the correct way to create a set in Python?
A. set = [1, 2, 3]
B. set = {1, 2, 3}
C. set = set({1, 2, 3})
D. Both B and C
d
In Python, we can create a set object by enclosing all the elements inside curly braces { }, separated by a comma. Another way of creating a set object in Python is by using a built-in set() function.
3. How do you create an empty set in Python?
A. {}
B. set()
C. []
D. ()
b
{} creates an empty dictionary, while set() creates an empty set.
4. What is the output of print(len({1, 2, 2, 3, 4, 4}))?
A. 3
B. 4
C. 5
D. 6
b
Sets do not allow duplicate values, so {1, 2, 2, 3} becomes {1, 2, 3}.
5. What will be the result of print(set(“hello”))?
A. {‘h’, ‘e’, ‘l’, ‘o’, ‘l’}
B. {‘hello’}
C. {‘e’, ‘h’, ‘l’, ‘o’}
D. [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
c
The set(“hello”) creates a set of unique characters from the string “hello”. The characters in “hello” are: ‘h’, ‘e’, ‘l’, ‘l’, ‘o’. Sinec sets automatically remove duplicates, so ‘l’ only appears once. The result is a set containing the characters { ‘h’, ‘e’, ‘l’, ‘o’ }.
6. Which of the following functions will remove an element from a set without raising an error if the element is not found?
A. s.remove(x)
B. s.discard(x)
C. s.pop(x)
D. Both A and B
b
The discard() function removes an element if present. But this function does nothing if it’s not found—no error is raised.
7. What is the output of print({1, 2, 3} & {3, 4, 5})?
A. {1, 2, 3, 4, 5}
B. {3}
C. {1, 2}
D. {1, 2, 4, 5}
b
The & operator performs an intersection of two sets, and returns only common elements found in both.
8. What is the output of print({1, 2, 3, 3} | {3, 4, 4, 5})?
A. {1, 2, 3, 4, 5}
B. {1, 2, 5}
C. {3, 3, 4, 4}
D. {1, 2, 3, 3, 4, 4, 5}
a
The | operator performs a union of two sets, which creates a new set by combining all unique elements.
9. What does a – b output in the following code?
a = {1, 2, 3} b = {3, 4, 5} print(a - b)
A. {1, 2}
B. {3}
C. {4, 5}
D. {}
a
The – operator performs a difference operation, removing elements of the second set from the first. In other words, a – b means elements in a that are not in b.
10. What is the result of a ^ b?
a = {1, 2, 3} b = {3, 4, 5}
A. {1, 2, 4, 5}
B. {3}
C. {1, 2}
D. {1, 2, 3, 4, 5}
a
The ^ operator performs a symmetric difference, which gives elements in either set but not both.
11. What is the output of print(bool(set())) in Python?
A. True
B. False
C. None
D. Error
b
An empty set is considered False when converted to a boolean.
12. Which of the following feature is true for Python sets?
- Mutable
- Unordered
- Unique Elements
- Heterogeneous Data
A. Only 1 and 2
B. Only 2 and 3
C. 1, 2, and 3
D. All 1, 2, 3, 4
d
All features are correct for Python sets.
13. Which method adds an element to a set?
A. append()
B. insert()
C. add()
D. update()
c
The add() method inserts a single element, while update() method adds multiple elements from an iterable. If the element already exists, nothing happens (no error).
14. Which of the following will raise a TypeError?
A. set([1, 2, 3])
B. set(“abc”)
C. set({[1, 2], [3, 4]})
D. set((1, 2, 3))
c
Sets can only store hashable (immutable) elements. Since [2, 3] is a list, and is mutable, Python raises an error.
15. What will be the output of the following program code if no error?
my_set = {25, 20.24, "Python"} print(my_set[1])
A. 20.24
B. 25
C. TypeError
D. IndexError
c
You cannot access directly using indexing or slicing. If you try to access an element using an index in a set, it will throw TypeError.
16. Will the following Python code execute without any error? If yes, what will be the output?
my_set = {2, 4, 6} my_set.add([6, 8, 10]) print(my_set)
A. {2, 4, 6, 8, 10}
B. {2, 4, [6, 8, 10]}
C. {2, 4, (6, 8, 10)}
D. TypeError
d
You cannot add a list of elements to a set since they are unhashable type. If you try, it will raise TypeError.
17. What happens when you run this Python code?
my_set = {1, 2, 3} my_set.add((4, 5)) print(my_set)
A. {(4, 5), 1, 2, 3}
B. {1, 2, 3, 4, 5}
C. {1, 2, 3, [4, 5]}
D. TypeError
a
You can add a tuple of elements to a set since they are hashable type.
The entire tuple (4, 5) is treated as one element.
The entire tuple (4, 5) is treated as one element.
18. What will be the output of the following Python code?
my_set = {2, 4, 6, 8} my_set.update([6, 8]) print(my_set)
A. {2, 4, [6, 8]}
B. {8, 2, 4, 6}
C. {2, 4, 6}
D. TypeError
b
If you add a list of elements using update() function in a set, the set always store unique elements only by ignoring duplicate elements.
19. What is the expected output of the following program code?
a = {1, 2, 3} b = {3, 2, 1} print(a == b)
A. True
B. False
C. TypeError
D. None
a
Sets are unordered collections. As long as they contain the same element, they are equal.
20. What is the output of this code?
s = {1, 2, 3} s.update("345") print(s)
A. {1, 2, 3, “345”}
B. {1, 2, 3, 4, 5}
C. {1, 2, 3, ‘4’, ‘5’}
D. {1, 2, 3, ‘4’, ‘5’, ‘3’}
d
If you add a string “345” using update() function in a set, the update() function will add each characters of the string as an individual element to a set because string is an iterable object. Therefore, ‘3’, ‘4’, and ‘5’ is added as a separate element.
21. What will this Python code print?
a = {1, 2, 3} b = a a.add(4) print(b)
A. {1, 2, 3}
B. {1, 2, 3, 4}
C. TypeError
D. None
b
Since b = a assigns the same reference, both a and b will point to the same set in memory.
22. Can the following Python code execute? If yes, what will be the output?
my_set = {1, 3, 5} my_set.update(5, 9, 11) print(my_set)
A. {1, 3, 5, 9, 11}
B. {1, 3, 5, (5, 9, 11)}
C. {1, 3, 5, (9, 11)}
D. TypeError
d
Since the update() function only supports iterable objects like list, tuple, etc., you cannot update integer values directly through update() function because int is not iterable object.
23. What is the result of this Python code?
a = {1, 2, 3} b = a.copy() a.add(4) print(b)
A. {1, 2, 3, 4}
B. {1, 2, 3}
C. {4}
D. TypeError
b
The copy() function creates a shallow copy. Changes to set a don’t affect set b.
24. Which of the following is not a valid set operation?
A. s1.union(s2)
B. s1 + s2
C. s1 & s2
D. s1.difference(s2)
b
You cannot use + operator to combine sets. Use | or union() instead.
25. What will this code print after execution?
a = {1, 2, 3} b = {4, 5, 6} print(a & b == set())
A. True
B. False
C. {}
D. TypeError
a
The operation a & b returns an empty set since there’s no overlap. An empty set equals set().
26. What will be the output of the following Python code?
my_set1 = {1, 2, 3, 4, 5, 6} my_set2 = {1, 2, 3, 4} result = my_set1.issubset(my_set2) print(result) result = my_set2 <= my_set1 print(result)
A. True True
B. True False
C. False True
D. False False
c
my_set1 <= my_set2 checks that if my_set1 is a subset of my_set2. The issubset() method returns true value if my_set1 is the subset of my_set2. Otherwise, it returns false value.
27. Which operation checks that if two sets have no common elements?
A. a & b == set()
B. a.isdisjoint(b)
C. a ^ b == a | b
D. Both A and B
d
The isdisjoint() function returns True if two sets have no common elements, and a & b == set() also achieves the same.
28. Which of the following is correct about a frozenset?
A. You can add elements to a frozenset using add().
B. You can remove elements from a frozenset using remove().
C. A frozenset is mutable.
D. A frozenset is immutable and hashable.
d
Unlike regular sets, frozenset is immutable and hashable, meaning that it cannot be changed after creation. That’s why it can be used as a dictionary key or set element.
29. What is the result of this code?
a = frozenset([1, 2]) b = set([a]) print(b)
A. set([frozenset([1, 2])])
B. {frozenset({1, 2})}
C. frozenset({1, 2})
D. Both B and C
b
You can put a frozenset inside a set because it’s hashable.
30. Which of the following is invalid?
A. frozenset([1, 2, 3])
B. dict({frozenset([1]): “a”})
C. set([frozenset([1, 2]), frozenset([3])])
D. frozenset([[1, 2], [3, 4]])
d
Lists are unhashable. You can’t put a list inside a frozenset because it needs hashable elements.
Quiz Results
0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score