Question 1:
What is a set in Python?
a) A collection of ordered elements
b) A collection of unordered elements
c) A single element
d) A data type
Question 2:
How do you create an empty set in Python?
a) set()
b) empty_set = {}
c) empty_set = set()
d) Both b and c
Question 3:
How do you add an element to a set in Python?
a) set.insert(element)
b) set.add(element)
c) set.append(element)
d) set.include(element)
Question 4:
What is the key difference between a set and a list in Python?
a) Sets are ordered, while lists are unordered
b) Sets are mutable, while lists are immutable
c) Sets can contain only numeric elements
d) Sets are unordered and do not allow duplicate elements
Question 5:
How do you check if an element is present in a set?
a) element in set
b) set.contains(element)
c) set.exists(element)
d) element.exists(set)
Question 6:
What happens when you try to add a duplicate element to a set?
a) The element is added successfully
b) Python raises an exception
c) The duplicate element is ignored, and the set remains unchanged
d) The set is automatically sorted
Question 7:
How do you remove an element from a set?
a) set.remove(element)
b) set.delete(element)
c) set.pop(element)
d) set.discard(element)
Question 8:
What is the purpose of the len() function when used with a set?
a) It returns the total number of elements in the set
b) It returns the last element of the set
c) It returns the length of each element in the set
d) It returns the sum of all elements in the set
Question 9:
How do you create a set with elements from 1 to 5 in Python?
a) set = {1, 2, 3, 4, 5}
b) set = range(1, 6)
c) set = set(1, 6)
d) set = {range(1, 6)}
Question 10:
What is the purpose of the pop() method in Python sets?
a) Removes the last element from the set
b) Removes a random element from the set and returns it
c) Removes the first occurrence of the specified element
d) Sorts the elements of the set
Question 11:
What is the difference between a set and a frozenset in Python?
a) Sets are mutable, while frozensets are immutable
b) Sets are unordered, while frozensets are ordered
c) Sets can contain only numeric elements
d) Sets allow duplicate elements, while frozensets do not
Question 12:
Which of the following statements is true regarding the union of two sets?
a) The union operator for sets is +
b) The union of two sets is the intersection of their elements
c) The union of two sets contains all unique elements from both sets
d) The union of two sets results in an empty set
Question 13:
What is the purpose of the clear() method in Python sets?
a) Clears all elements from the set
b) Returns a clear copy of the set
c) Clears only the first element from the set
d) Clears the set if a specific element is provided
Question 14:
Which method is used to find the intersection of two sets in Python?
a) set.intersection(set2)
b) set.intersect(set2)
c) set.common(set2)
d) set.and(set2)
Question 15:
What is 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) {1, 2, 3}
c) {3, 4, 5}
d) {1, 2, 4, 5}
Question 16:
How do you check if a set is a subset of another set?
a) set.is_subset(other_set)
b) set.subset_of(other_set)
c) set.issubset(other_set)
d) set.contains_subset(other_set)
Question 17:
What does the difference() method do when applied to two sets?
a) Returns the union of the two sets
b) Returns the intersection of the two sets
c) Returns the difference between the two sets
d) Returns the symmetric difference between the two sets
Question 18:
What is the purpose of the symmetric_difference() method in Python sets?
a) Returns the union of the two sets
b) Returns the intersection of the two sets
c) Returns the difference between the two sets
d) Returns the symmetric difference between the two sets
Question 19:
What is the output of the following code?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.difference(set2)
print(result)
a) {1, 2, 3, 4, 5}
b) {1, 2}
c) {3}
d) {4, 5}
Question 20:
What is the purpose of the issuperset() method in Python sets?
a) Checks if the set is a proper superset of another set
b) Checks if the set is a subset of another set
c) Checks if the set is equal to another set
d) Checks if the set contains all elements of another set
Answer Key:
- b) A collection of unordered elements
- c) empty_set = set()
- b) set.add(element)
- d) Sets are unordered and do not allow duplicate elements
- a) element in set
- c) The duplicate element is ignored, and the set remains unchanged
- a) set.remove(element)
- a) It returns the total number of elements in the set
- a) set = {1, 2, 3, 4, 5}
- b) Removes a random element from the set and returns it
- a) Sets are mutable, while frozensets are immutable
- c) The union of two sets contains all unique elements from both sets
- a) Clears all elements from the set
- a) set.intersection(set2)
- a) {1, 2, 3, 4, 5}
- c) set.issubset(other_set)
- d) Returns the symmetric difference between the two sets
- d) Returns the symmetric difference between the two sets
- b) {1, 2}
- a) Checks if the set is a proper superset of another set