Learn with Yasir

Share Your Feedback

Python Sets MCQs – Multiple Choice Questions for Practice and Learning

Test and improve your understanding of Python sets with these multiple choice questions. Practice set operations, membership, and manipulation with beginner-friendly MCQs and detailed answers. Ideal for students and Python learners.

Topic: sets


📝 Multiple Choice Questions

🟢 Beginner

Q1. Which of the following is a correct way to create a set in Python?

  • 🟢 A. myset = {}
  • 🔵 B. myset = []
  • 🟠 C. myset = set()
  • 🔴 D. myset = dict()
Answer

myset = set()

Using set() creates an empty set. Using {} creates an empty dictionary.


Q2. What is the output of the following code?

myset = {1, 2, 2, 3}
print(len(myset))
  • 🟢 A. 2
  • 🔵 B. 3
  • 🟠 C. 4
  • 🔴 D. 1
Answer

3

Sets store only unique elements, so duplicate 2 is ignored.


Q3. Which method is used to add an item to a set?

  • 🟢 A. append()
  • 🔵 B. add()
  • 🟠 C. insert()
  • 🔴 D. extend()
Answer

add()

The add() method adds a single element to a set.


Q4. What will be the output?

a = {1, 2, 3}
a.add(2)
print(a)
  • 🟢 A. {1, 2, 3, 2}
  • 🔵 B. {1, 2, 3}
  • 🟠 C. {1, 3}
  • 🔴 D. {1, 2}
Answer

{1, 2, 3}

Adding an existing element has no effect because sets contain only unique items.


Q5. Which method removes an element from a set without raising an error if the element does not exist?

  • 🟢 A. remove()
  • 🔵 B. discard()
  • 🟠 C. pop()
  • 🔴 D. clear()
Answer

discard()

The discard() method does not raise an error if the item is not present.


Q6. Which of these is true about sets?

  • 🟢 A. Sets are ordered collections
  • 🔵 B. Sets can contain duplicate elements
  • 🟠 C. Sets are mutable
  • 🔴 D. Sets can contain lists
Answer

Sets are mutable

You can add and remove elements, but elements must be immutable.


Q7. Which operator is used for set union?

  • 🟢 A. +
  • 🔵 B. &
  • 🟠 C. |
  • 🔴 D. -
Answer

|

The '|' operator returns the union of two sets.


Q8. What is the output?

a = {1, 2}
b = {2, 3}
print(a | b)
  • 🟢 A. {1, 2, 3}
  • 🔵 B. {2}
  • 🟠 C. {1, 3}
  • 🔴 D. {1, 2}
Answer

{1, 2, 3}

Union combines elements from both sets, removing duplicates.


Q9. Which method removes all items from a set?

  • 🟢 A. delete()
  • 🔵 B. remove()
  • 🟠 C. clear()
  • 🔴 D. discard()
Answer

clear()

clear() empties the set completely.


Q10. Which method can be used to get a copy of a set?

  • 🟢 A. copy()
  • 🔵 B. clone()
  • 🟠 C. duplicate()
  • 🔴 D. replicate()
Answer

copy()

copy() returns a shallow copy of the set.


Q11. Which set operation returns elements common to both sets?

  • 🟢 A. Union
  • 🔵 B. Difference
  • 🟠 C. Intersection
  • 🔴 D. Symmetric Difference
Answer

Intersection

Intersection returns common elements.


Q12. What is the output?

a = {1, 2, 3}
b = {3, 4, 5}
print(a & b)
  • 🟢 A. {1, 2, 3, 4, 5}
  • 🔵 B. {3}
  • 🟠 C. {1, 2}
  • 🔴 D. {}
Answer

{3}

The '&' operator returns the intersection of the sets.


Q13. Which operation returns elements that are in either set but not both?

  • 🟢 A. Union
  • 🔵 B. Intersection
  • 🟠 C. Difference
  • 🔴 D. Symmetric Difference
Answer

Symmetric Difference

Symmetric difference excludes common elements.


Q14. What does the pop() method do on a set?

  • 🟢 A. Removes and returns an arbitrary element
  • 🔵 B. Removes a specific element
  • 🟠 C. Removes all elements
  • 🔴 D. Adds an element
Answer

Removes and returns an arbitrary element

Sets are unordered; pop() removes any item.


Q15. What is the output?

myset = {1, 2, 3}
myset.remove(4)
  • 🟢 A. Removes nothing
  • 🔵 B. Raises KeyError
  • 🟠 C. Removes 4
  • 🔴 D. Adds 4
Answer

Raises KeyError

remove() raises an error if the element is not found.


Q16. What is the result of set([1, 2, 2, 3])?

  • 🟢 A. [1, 2, 2, 3]
  • 🔵 B. {1, 2, 2, 3}
  • 🟠 C. {1, 2, 3}
  • 🔴 D. (1, 2, 3)
Answer

{1, 2, 3}

The set() constructor removes duplicates.


Q17. Which set method returns the difference of two sets?

  • 🟢 A. intersection()
  • 🔵 B. difference()
  • 🟠 C. union()
  • 🔴 D. symmetric_difference()
Answer

difference()

difference() returns elements only in the first set.


Q18. What is the output?

a = {1, 2, 3}
b = {2, 3, 4}
print(a - b)
  • 🟢 A. {1, 4}
  • 🔵 B. {2, 3}
  • 🟠 C. {1}
  • 🔴 D. {}
Answer

{1}

Difference returns items in a but not in b.


Q19. What is the data type of { } in Python?

  • 🟢 A. Set
  • 🔵 B. List
  • 🟠 C. Tuple
  • 🔴 D. Dictionary
Answer

Dictionary

Empty {} is a dictionary. Use set() for an empty set.


Q20. Which is a valid way to check if an item exists in a set?

fruits = {"apple", "banana"}
print("apple" ____ fruits)
  • 🟢 A. has
  • 🔵 B. in
  • 🟠 C. exists
  • 🔴 D. inside
Answer

in

Use 'in' to check membership in sets.


Q21. What is the output of the following code snippet?

fruits = {"apple", "banana", "cherry"}
fruits.add("apple")
print(len(fruits))
  • 🟢 A. 1
  • 🔵 B. 2
  • 🟠 C. 3
  • 🔴 D. 4
Answer

3

Sets only store unique elements, so adding 'apple' again doesn't change the set size.


🟡 Intermediate

Q1. Which operation will return the elements present in both sets?

set1 = {1, 2, 3}
set2 = {2, 3, 4}
  • 🟢 A. set1 - set2
  • 🔵 B. set1 | set2
  • 🟠 C. set1 & set2
  • 🔴 D. set1 ^ set2
Answer

set1 & set2

The & operator performs intersection, returning elements common to both sets.


Q2. What does the symmetric_difference() method return?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
  • 🟢 A. Elements in either set but not in both
  • 🔵 B. Elements common to both sets
  • 🟠 C. Elements only in the first set
  • 🔴 D. All elements from both sets
Answer

Elements in either set but not in both

symmetric_difference() returns elements that are in exactly one of the sets.


🔴 Advanced

Q1. What is the output of this set comprehension?

word = "mississippi"
letters = {x for x in word if word.count(x) > 2}
print(letters)
  • 🟢 A. {'m', 'i', 's', 's', 'i', 'p', 'p', 'i'}
  • 🔵 B. {'i', 's', 'p'}
  • 🟠 C. {'i', 's'}
  • 🔴 D. {'p'}
Answer

{'i', 's'}

The comprehension creates a set of unique letters appearing more than twice in the word.


🧠 Practice & Progress

Explore More Topics

📘 Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules