30+ MCQs on Python Sets and Sets Operations

Ayushi Trivedi Last Updated : 06 Mar, 2024
8 min read

Welcome to the Python Sets and Set Operations Python Interview Questions Sets are unordered collections of unique elements in Python, offering powerful operations for set manipulation such as union, intersection, difference, and more. These questions will test your knowledge of various set operations and techniques, including adding elements, removing duplicates, performing set operations, and checking membership. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore the world of Python sets and set operations together!

Python Sets

Q1. What is a set in Python?

a) A data structure that stores elements in key-value pairs

b) A data structure that stores elements in an ordered sequence

c) A collection of unique and unordered elements

d) A collection of elements with duplicates allowed

Answer: c

Explanation: In Python, a set is a collection of unique and unordered elements.

Q2. How do you create an empty set in Python?

a) Using curly braces: {}

b) Using square brackets: []

c) Using parentheses: ()

d) There is no way to create an empty set

Answer: a

Explanation: An empty set in Python is created using curly braces: {}.

Q3. Which of the following is a valid way to create a set containing integers 1, 2, and 3?

a) {1, 2, 3}

b) set(1, 2, 3)

c) [1, 2, 3]

d) (1, 2, 3)

Answer: a

Explanation: The syntax {1, 2, 3} creates a set containing the integers 1, 2, and 3.

Q4. What will be the output of the following code?

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)

a) {1, 2, 3}

b) {1, 2, 3, 4}

c) {1, 2, 3, [4]}

d) Error, sets do not support the add method

Answer: b

Explanation: The add method is used to add elements to a set. In this code, 4 is added to my_set.

Q5. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2)
print(result)

a) {1, 2, 3, 4, 5}

b) {3}

c) {1, 2, 3}

d) {1, 2, 3, 4, 5, 6}

Answer: a

Explanation: The union method combines two sets and removes duplicates, resulting in {1, 2, 3, 4, 5}.

Q6. What does the intersection of two sets represent?

a) All elements that are common to both sets

b) All elements that are unique to the first set

c) All elements that are unique to the second set

d) All elements from both sets without duplicates

Answer: a

Explanation: The intersection of two sets contains all elements that are common to both sets.

Q7. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.intersection(set2)
print(result)

a) {3}

b) {}

c) {1, 2, 3}

d) {3, 4, 5}

Answer: a

Explanation: The intersection method finds the common elements between set1 and set2, resulting in {3}.

Q8. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.difference(set2)
print(result)

a) {1, 2}

b) {3}

c) {4, 5}

d) {1, 2, 3, 4, 5}

Answer: a

Explanation: The difference method returns the elements that are in set1 but not in set2, resulting in {1, 2}.

Q9. What does the symmetric_difference of two sets represent?

a) All elements that are common to both sets

b) All elements that are unique to the first set

c) All elements that are unique to the second set

d) All elements that are not common to both sets

Answer: d

Explanation: The symmetric difference of two sets contains all elements that are not common to both sets.

Q10. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.symmetric_difference(set2)
print(result)

a) {1, 2}

b) {3}

c) {1, 2, 4, 5}

d) {1, 2, 3, 4, 5}

Answer: c

Explanation: The symmetric_difference method returns the elements that are in either set1 or set2, but not in both, resulting in {1, 2, 4, 5}.

Q11. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2)
print(set1)

a) {1, 2, 3}

b) {3, 4, 5}

c) {1, 2, 3, 4, 5}

d) Error, sets do not support the update method

Answer: c

Explanation: The update method adds all elements from set2 to set1, resulting in {1, 2, 3, 4, 5}.

Q12. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.intersection_update(set2)
print(set1)

a) {1, 2, 3}

b) {3}

c) {}

d) Error, sets do not support the intersection_update method

Answer: b

Explanation: The intersection_update method updates set1 with the intersection of set1 and set2, resulting in {3}.

Q13. What does the issubset() method do for sets in Python?

a) Checks if one set is a subset of another set

b) Checks if one set is a superset of another set

c) Checks if two sets have the same elements

d) Checks if two sets have no elements in common

Answer: a

Explanation: The issubset() method checks if one set is a subset of another set.

Q14. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {2, 3}
result = set1.issubset(set2)
print(result)

a) True

b) False

c) Error, sets do not support the issubset method

d) None

Answer: b

Explanation: set1 is not a subset of set2 because it contains elements not in set2, so result is False.

Q15. What does the issuperset() method do for sets in Python?

a) Checks if one set is a subset of another set

b) Checks if one set is a superset of another set

c) Checks if two sets have the same elements

d) Checks if two sets have no elements in common

Answer: b

Explanation: The issuperset() method checks if one set is a superset of another set.

Q16. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {2, 3}
result = set1.issuperset(set2)
print(result)

a) True

b) False

c) Error, sets do not support the issuperset method

d) None

Answer: a

Explanation: set1 is a superset of set2 because all elements of set2 are in set1, so result is True.

Q17. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {4, 5}
result = set1.isdisjoint(set2)
print(result)

a) True

b) False

c) Error, sets do not support the isdisjoint method

d) None

Answer: a

Explanation: set1 and set2 have no elements in common, so result is True.

Q18. What does the copy() method do for sets in Python?

a) Creates a shallow copy of the set

b) Creates a deep copy of the set

c) Adds an element to the set

d) Removes an element from the set

Answer: a

Explanation: The copy() method creates a shallow copy of the set.

Q19. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = set1.copy()
set2.add(4)
print(set1)
print(set2)

a) {1, 2, 3}, {1, 2, 3, 4}

b) {1, 2, 3, 4}, {1, 2, 3, 4}

c) {1, 2, 3}, {4}

d) Error, sets do not support the copy method

Answer: a

Explanation: set2 is a copy of set1, so adding 4 to set2 does not affect set1.

Q20. What does the clear() method do for sets in Python?

a) Removes all elements from the set

b) Deletes the set entirely

c) Creates a new empty set

d) Updates the set with new elements

Answer: a

Explanation: The clear() method removes all elements from the set, leaving it empty.

Q21. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {4, 5}
set1.clear()
print(set1)

a) {}

b) {1, 2, 3}

c) {4, 5}

d) Error, sets do not support the clear method

Answer: a

Explanation: The clear() method removes all elements from set1, resulting in an empty set.

Q22. What is the correct way to remove an element from a set?

a) Using the remove() method

b) Using the discard() method

c) Using the pop() method

d) All of the above

Answer: d

Explanation: All of the mentioned methods (remove(), discard(), and pop()) can be used to remove elements from a set.

Q23. What will be the output of the following code?

set1 = {1, 2, 3}
set1.remove(2)
print(set1)

a) {1, 2, 3}

b) {1, 3}

c) {2, 3}

d) Error, element 2 not found in set1

Answer: b

Explanation: The remove() method removes the specified element (2 in this case) from the set, resulting in {1, 3}.

Q24. What will be the output of the following code?

set1 = {1, 2, 3}
set1.discard(4)
print(set1)

a) {1, 2, 3}

b) {1, 2, 3, 4}

c) {1, 2, 3}

d) Error, element 4 not found in set1

Answer: a

Explanation: The discard() method tries to remove the specified element (4 in this case) from the set, but since 4 is not in the set, it does nothing, resulting in {1, 2, 3}.

Q25. What be the output of the following code?

set1 = {1, 2, 3}
element = set1.pop()
print(element)
print(set1)

a) 1, {2, 3}

b) 1, {1, 2, 3}

c) 3, {1, 2}

d) Error, pop() takes no arguments

Answer: a

Explanation: The pop() method removes and returns an arbitrary element from the set, in this case, it removes 1, resulting in {2, 3}.

Q26. What does the difference_update() method do for sets in Python?

a) Updates the set with the difference of itself and another set

b) Updates the set with the symmetric difference of itself and another set

c) Updates the set with the union of itself and another set

d) Updates the set with the intersection of itself and another set

Answer: a

Explanation: The difference_update() method updates the set with the difference of itself and another set.

Q27. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {2, 3, 4}
set1.difference_update(set2)
print(set1)

a) {1}

b) {2, 3}

c) {1, 2, 3, 4}

d) {4}

Answer: a

Explanation: The difference_update() method updates set1 with the elements that are in set1 but not in set2, resulting in {1}.

Q28. What does the symmetric_difference_update() method do for sets in Python?

a) Updates the set with the difference of itself and another set

b) Updates the set with the symmetric difference of itself and another set

c) Updates the set with the union of itself and another set

d) Updates the set with the intersection of itself and another set

Answer: b

Explanation: The symmetric_difference_update() method updates the set with the symmetric difference of itself and another set.

Q29. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {2, 3, 4}
set1.symmetric_difference_update(set2)
print(set1)

a) {1, 2, 3, 4}

b) {1, 4}

c) {1}

d) {2, 3}

Answer: b

Explanation: The symmetric_difference_update() method updates set1 with the elements that are in either set1 or set2, but not in both, resulting in {1, 4}.

Q30. What does the isdisjoint() method do for sets in Python?

a) Checks if one set is a subset of another set

b) Checks if one set is a superset of another set

c) Checks if two sets have no elements in common

d) Checks if two sets have the same elements

Answer: c

Explanation: The isdisjoint() method checks if two sets have no elements in common.

Q31. What will be the output of the following code?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1 + set2
print(set3)

a) {1, 2, 3, 4, 5}

b) {1, 2, 3, 3, 4, 5}

c) Error, sets do not support the + operator for concatenation

d) Error, sets do not allow duplicates

Answer: c

Explanation: Sets do not support the + operator for concatenation, so trying to use it will result in an error.

Q32. What will be the output of the following code?

set1 = {1, 2, 3}
set1.remove(4)
print(set1)

a) {1, 2, 3}

b) {1, 2, 3, 4}

c) {1, 2, 3}

d) Error, element 4 not found in set1

Answer: d

Explanation: The remove() method will raise an error if the specified element is not found in the set.

Q33. What will be the output of the following code?

my_set = {1, 2, 3}
my_set[0]

a) 1

b) 2

c) 3

d) Error, sets do not support indexing

Answer: d

Explanation: Sets do not support indexing, so trying to access elements by index will result in an error.

Congratulations on completing the Python Sets and Set Operations MCQs! Sets are versatile data structures in Python, offering efficient methods for dealing with unique collections of elements. By mastering set operations, you gain the ability to perform tasks such as finding common elements, removing duplicates, and checking for differences between sets. Keep practicing and experimenting with Python’s set functionalities to become proficient in handling sets within your programs. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!

You can also enroll in our free Python Course Today!

Read our more articles related to MCQs in Python:

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details