Exercise: Python

Questions for: Sets

How can you check if two sets have no elements in common?
A:
set.is_not_common(other_set)
B:
set.not_common(other_set)
C:
set.isdisjoint(other_set)
D:
set.disjoint(other_set)
Answer: C
The isdisjoint() method checks if two sets have no elements in common.
What is the result of the expression set('programming') | set('python')?
A:
{'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n'}
B:
{'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n', 'y', 't', 'h'}
C:
['p', 'r', 'o', 'g', 'a', 'm', 'i', 'n']
D:
('programming', 'python')
Answer: B
The '|' operator performs the union of two sets, combining unique elements.
Which method is used to remove all elements from a set?
A:
set.clear()
B:
set.remove_all()
C:
set.delete_all()
D:
set.empty()
Answer: A
The clear() method is used to remove all elements from a set.
What is the purpose of the set.update(iterable) method?
A:
Adds elements to the set.
B:
Removes elements from the set.
C:
Updates the set with elements from another set.
D:
Raises a TypeError since sets are immutable.
Answer: A
The update() method is used to add elements to the set.
Which of the following methods is used to check if a set is a subset of another set?
A:
set.is_subset(other_set)
B:
set.contains(other_set)
C:
set.issubset(other_set)
D:
set.subset(other_set)
Answer: C
The issubset() method is used to check if a set is a subset of another set.
Ad Slot (Above Pagination)
Quiz