Python Programming - Sets

Exercise : Sets - General Questions
  • Sets - General Questions
6.
How do you create an empty set?
empty_set = set()
empty_set = {}
empty_set = set([])
empty_set = set('')
Answer: Option
Explanation:
An empty set can be created using the set() constructor.

7.
Which of the following methods is used to remove and return an arbitrary element from a set?
set.remove(element)
set.discard(element)
set.pop()
set.delete(element)
Answer: Option
Explanation:
The pop() method removes and returns an arbitrary element from the set.

8.
What is the result of the expression set([1, 2, 2, 3, 4, 4, 5])?
{1, 2, 3, 4, 5}
[1, 2, 3, 4, 5]
set([1, 2, 3, 4, 5])
({1, 2, 2, 3, 4, 4, 5})
Answer: Option
Explanation:
A set automatically removes duplicate elements, resulting in {1, 2, 3, 4, 5}.

9.
Which of the following methods is used to check if a set is a subset of another set?
set.is_subset(other_set)
set.contains(other_set)
set.issubset(other_set)
set.subset(other_set)
Answer: Option
Explanation:
The issubset() method is used to check if a set is a subset of another set.

10.
What is the purpose of the set.update(iterable) method?
Adds elements to the set.
Removes elements from the set.
Updates the set with elements from another set.
Raises a TypeError since sets are immutable.
Answer: Option
Explanation:
The update() method is used to add elements to the set.