Exercise: Python

Questions for: Sets

How do you convert a list into a set?
A:
set(list_variable)
B:
list_variable.to_set()
C:
list_variable.as_set()
D:
convert_set(list_variable)
Answer: A
The set() constructor can be used to convert a list into a set.
How do you find the elements that are unique to each set?
A:
set.symmetric_difference(other_set)
B:
set.difference(other_set)
C:
set.unique_elements(other_set)
D:
set.unique_difference(other_set)
Answer: A
The symmetric_difference() method returns elements that are unique to each set.
Which method is used to remove and return an arbitrary element from a set?
A:
set.pop()
B:
set.remove()
C:
set.extract()
D:
set.take()
Answer: A
The pop() method removes and returns an arbitrary element from a set.
What is the result of the expression set([1, 2, 3, 4]).intersection(set([3, 4, 5, 6]))?
A:
{3, 4}
B:
{}
C:
{1, 2, 3, 4, 5, 6}
D:
{1, 2}
Answer: A
The intersection() method finds the common elements between two sets.
What is the result of the expression set('python') | set('java')?
A:
{'p', 'y', 't', 'h', 'o', 'n', 'j'}
B:
{'p', 'y', 't', 'h', 'o', 'n'}
C:
{'j', 'a', 'v'}
D:
{'j', 'p', 't', 'o', 'h', 'n', 'a', 'y', 'v'}
Answer: D
The '|' operator performs the union of two sets, combining unique elements.
Ad Slot (Above Pagination)
Quiz