Python Programming - Sets

Exercise : Sets - General Questions
  • Sets - General Questions
16.
How do you find the difference between two sets?
set.difference(other_set)
set.subtract(other_set)
set.diff(other_set)
set.diffother_set
Answer: Option
Explanation:
The difference() method is used to find the difference between two sets.

17.
What is the result of the expression set('python').intersection(set('java'))?
set()
{'a'}
{'p'}
{'j', 'a', 'v'}
Answer: Option
Explanation:
The intersection() method finds common elements, and in this case, there are none.

18.
What is the purpose of the set.discard(element) method?
Removes the specified element from the set.
Removes the last element from the set.
Removes all occurrences of the specified element.
Raises a TypeError since sets are immutable.
Answer: Option
Explanation:
The discard() method removes the specified element from the set, if present.

19.
Which method is used to find the union of two sets?
set.combine(other_set)
set.union(other_set)
set.merge(other_set)
set.join(other_set)
Answer: Option
Explanation:
The union() method is used to find the union of two sets.

20.
Which of the following statements about frozensets in Python is correct?
Frozensets are mutable.
Frozensets can be used as dictionary keys.
Frozensets can be modified after creation.
Frozensets support element removal using the remove() method.
Answer: Option
Explanation:
Frozensets are immutable and can be used as dictionary keys or elements of other sets.