Python Programming - Sets

Exercise : Sets - General Questions
  • Sets - General Questions
11.
Which method is used to remove all elements from a set?
set.clear()
set.remove_all()
set.delete_all()
set.empty()
Answer: Option
Explanation:
The clear() method is used to remove all elements from a set.

12.
What is the result of the expression set('programming') | set('python')?
{'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n'}
{'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n', 'y', 't', 'h'}
['p', 'r', 'o', 'g', 'a', 'm', 'i', 'n']
('programming', 'python')
Answer: Option
Explanation:
The '|' operator performs the union of two sets, combining unique elements.

13.
How can you check if two sets have no elements in common?
set.is_not_common(other_set)
set.not_common(other_set)
set.isdisjoint(other_set)
set.disjoint(other_set)
Answer: Option
Explanation:
The isdisjoint() method checks if two sets have no elements in common.

14.
What is the purpose of the set.symmetric_difference(other_set) method?
Returns the common elements between two sets.
Returns the union of two sets.
Returns the difference between two sets.
Returns the symmetric difference between two sets.
Answer: Option
Explanation:
The symmetric_difference() method returns the elements that are unique to each set.

15.
What does the frozenset() function do?
Creates an immutable set.
Creates an empty set.
Converts a set to a list.
Creates a frozen set with specified elements.
Answer: Option
Explanation:
The frozenset() function is used to create an immutable set in Python.