Exercise: Python

Questions for: Sets

Which method is used to remove a specific element from a set, raising an error if the element is not present?
A:
set.remove(element)
B:
set.discard(element)
C:
set.exclude(element)
D:
set.delete(element)
Answer: A
The remove() method removes a specific element, raising an error if the element is not present.
How do you find the elements common to multiple sets?
A:
set.intersection_all()
B:
set.common()
C:
set.common_elements()
D:
set.intersection(*sets)
Answer: D
The intersection() method with the asterisk (*) operator is used to find common elements in multiple sets.
What is the result of the expression set('python').isdisjoint(set('java'))?
A:
True
B:
False
C:
Raises a TypeError
D:
None of the above
Answer: A
The isdisjoint() method returns True if two sets have no elements in common.
What does the set.union(other_set) method do?
A:
Finds the common elements between two sets.
B:
Combines two sets and removes duplicate elements.
C:
Returns the elements that are unique to each set.
D:
Checks if two sets are equal.
Answer: B
The union() method combines two sets and removes duplicate elements.
What is the result of the expression len(set())?
A:
0
B:
1
C:
Raises a TypeError
D:
None of the above
Answer: A
An empty set has a length of 0.
Ad Slot (Above Pagination)
Quiz