Python Programming - Sets

Exercise : Sets - General Questions
  • Sets - General Questions
1.
What is the result of the expression set('python')?
{'p', 'y', 't', 'h', 'o', 'n'}
('p', 'y', 't', 'h', 'o', 'n')
['p', 'y', 't', 'h', 'o', 'n']
('python')
Answer: Option
Explanation:
The set() constructor converts a string into a set of its characters.

2.
Which method is used to add an element to a set?
set.append(element)
set.insert(element)
set.add(element)
set.update(element)
Answer: Option
Explanation:
The add() method is used to add a single element to a set.

3.
What is the purpose of the set.remove(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 remove() method removes the specified element from the set.

4.
How can you find the intersection of two sets?
set1.join(set2)
set1 & set2
set1.intersect(set2)
set1.intersection(set2)
Answer: Option
Explanation:
The intersection() method is used to find the common elements between two sets.

5.
Which of the following statements about sets in Python is not correct?
Elements within a set are unordered.
Sets can contain duplicate elements.
Sets can not be accessed using indices.
Sets are mutable.
Answer: Option
Explanation:
Sets in Python do not allow duplicate elements.