Exercise: Python

Questions for: Sets

What is the purpose of the set.discard(element) method?
A:
Removes the specified element from the set.
B:
Removes the last element from the set.
C:
Removes all occurrences of the specified element.
D:
Raises a TypeError since sets are immutable.
Answer: A
The discard() method removes the specified element from the set, if present.
What is the result of the expression set('python').intersection(set('java'))?
A:
set()
B:
{'a'}
C:
{'p'}
D:
{'j', 'a', 'v'}
Answer: A
The intersection() method finds common elements, and in this case, there are none.
How do you find the difference between two sets?
A:
set.difference(other_set)
B:
set.subtract(other_set)
C:
set.diff(other_set)
D:
set.diffother_set
Answer: A
The difference() method is used to find the difference between two sets.
What does the frozenset() function do?
A:
Creates an immutable set.
B:
Creates an empty set.
C:
Converts a set to a list.
D:
Creates a frozen set with specified elements.
Answer: A
The frozenset() function is used to create an immutable set in Python.
What is the purpose of the set.symmetric_difference(other_set) method?
A:
Returns the common elements between two sets.
B:
Returns the union of two sets.
C:
Returns the difference between two sets.
D:
Returns the symmetric difference between two sets.
Answer: D
The symmetric_difference() method returns the elements that are unique to each set.
Ad Slot (Above Pagination)
Quiz