# Python Sets MCQ Questions and Answers

Sets in Python are an unordered collection of unique elements. They are mutable, allowing the addition and removal of elements, but the elements themselves must be immutable. Sets are useful for membership testing, removing duplicates from a sequence, and performing mathematical operations such as unions, intersections, differences, and symmetric differences. Here we present 15 multiple-choice questions to test your knowledge of Python Sets. Each MCQ has the correct answer with an explanation.

## 1. What is a set in Python?

a) A collection of key-value pairs
b) An ordered collection of unique elements
c) An unordered collection of unique elements
d) A mutable sequence of characters

c) An unordered collection of unique elements

### Explanation:

A set in Python is an unordered collection of unique elements, meaning it does not maintain any order and does not allow duplicates.

## 2. How do you create an empty set in Python?

a) set = {}
b) set = set()
c) set = []
d) set = ()

b) set = set()

### Explanation:

An empty set is created using the set() function. {} creates an empty dictionary, not a set.

## 3. Which of the following is a valid set in Python?

a) {1, 2, 2, 3}
b) {1: 'a', 2: 'b'}
c) {1, [2, 3], 4}
d) {(1, 2), (3, 4)}

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

### Explanation:

Sets can contain immutable elements like tuples but cannot contain mutable elements like lists. Sets automatically remove duplicate elements.

## 4. How do you add an item '4' to a set named 'my_set'?

b) my_set.append(4)
c) my_set.insert(4)
d) my_set.push(4)

### Explanation:

The add() method is used to add a single element to a set.

## 5. How do you remove an item '3' from a set 'my_set' if you're unsure it exists?

a) my_set.remove(3)
c) del my_set[3]
d) my_set.pop(3)

### Explanation:

discard() removes an element if it exists in the set, but does not raise an error if it doesn't, unlike remove().

## 6. How can you loop through a set 'my_set' to print all its elements?

a) for item in my_set: print(item)
b) for i in range(len(my_set)): print(my_set[i])
c) while my_set: print(my_set.pop())
d) print(my_set)

a) for item in my_set: print(item)

### Explanation:

Looping directly through the set using a for loop is the correct method to access each element.

## 7. How do you create a union of two sets 'set1' and 'set2'?

a) set1.union(set2)
b) set1 + set2
c) set1.join(set2)
d) set1 && set2

a) set1.union(set2)

### Explanation:

The union() method returns a new set with all elements from both sets.

## 8. What does the set method intersection() do?

a) Combines two sets
b) Returns the common elements of two sets
c) Removes all elements of another set from one set
d) Duplicates the set

b) Returns the common elements of two sets

### Explanation:

intersection() returns a set containing only elements that are common to both sets.

## 9. How do you check if 'my_set' is a superset of another set 'another_set'?

a) my_set.issuperset(another_set)
b) my_set > another_set
c) my_set == another_set
d) my_set.contains(another_set)

a) my_set.issuperset(another_set)

### Explanation:

The method issuperset() checks if the set has every element of the other set.

## 10. How do you remove all items from a set 'my_set'?

a) my_set.remove()
c) my_set.clear()
d) del my_set

c) my_set.clear()

### Explanation:

The clear() method removes all items from a set, leaving it empty.

## 11. Which method finds the difference between two sets?

a) difference()
b) subtract()
c) minus()
d) remove()

a) difference()

### Explanation:

difference() returns a set containing elements that are in the first set but not in the second.

## 12. What does set.pop() do?

a) Returns and removes a random element from the set
b) Removes the last element of the set
c) Deletes the set
d) Removes the first element of the set

a) Returns and removes a random element from the set

### Explanation:

pop() removes and returns a random element from the set. Sets are unordered, so there is no 'first' or 'last' element.

## 13. How do you check if two sets 'set1' and 'set2' have no elements in common?

a) set1.isdisjoint(set2)
b) set1.intersection(set2) == set()
c) set1 == set2
d) both a and b

d) both a and b

### Explanation:

isdisjoint() checks if two sets have no elements in common. An empty intersection also indicates no common elements.

## 14. What is the output of len({1, 2, 3, 4, 4})?

a) 5
b) 4
c) 3
d) Error

b) 4

### Explanation:

Sets do not contain duplicate elements. The set {1, 2, 3, 4, 4} actually is {1, 2, 3, 4}.

a) {1, 4}
b) {2, 3}
c) {1}
d) {1, 2, 3, 4}