Python Multiple Choice Questions on “Sets – 5”.
1. What will be the output of the following Python code?
s1={3, 4} s2={1, 2} s3=set() i=0 j=0 for i in s1: for j in s2: s3.add((i,j)) i+=1 j+=1 print(s3)
a) {(3, 4), (1, 2)}
b) Error
c) {(4, 2), (3, 1), (4, 1), (5, 2)}
d) {(3, 1), (4, 2)}
Answer: c
Clarification: The code shown above finds the Cartesian product of the two sets, s1 and s2. The Cartesian product of these two sets is stored in a third set, that is, s3. Hence the output of this code is: {(4, 2), (3, 1), (4, 1), (5, 2)}.
2. The ____________ function removes the first element of a set and the last element of a list.
a) remove
b) pop
c) discard
d) dispose
Answer: b
Clarification: The function pop removes the first element when used on a set and the last element when used to a list.
3. The difference between the functions discard and remove is that:
a) Discard removes the last element of the set whereas remove removes the first element of the set
b) Discard throws an error if the specified element is not present in the set whereas remove does not throw an error in case of absence of the specified element
c) Remove removes the last element of the set whereas discard removes the first element of the set
d) Remove throws an error if the specified element is not present in the set whereas discard does not throw an error in case of absence of the specified element
Answer: d
Clarification: The function remove removes the element if it is present in the set. If the element is not present, it throws an error. The function discard removes the element if it is present in the set. If the element is not present, no action is performed (Error is not thrown).
4. What will be the output of the following Python code?
s1={1, 2, 3} s2={3, 4, 5, 6} s1.difference(s2) s2.difference(s1)
a)
{1, 2}
{4, 5, 6}
b)
{1, 2}
{1, 2}
c)
{4, 5, 6}
{1, 2}
d)
{4, 5, 6}
{4, 5, 6}
View Answer
Answer: a
Clarification: The function s1.difference(s2) returns a set containing the elements which are present in the set s1 but not in the set s2. Similarly, the function s2.difference(s1) returns a set containing elements which are present in the set s2 but not in the set s1. Hence the output of the code shown above will be:
{1, 2}
{4, 5, 6}.
5. What will be the output of the following Python code?
s1={1, 2, 3} s2={4, 5, 6} s1.isdisjoint(s2) s2.isdisjoint(s1)
a)
True False
b)
False True
c)
True True
d)
False False
View Answer
Answer: c
Clarification: The function isdisjoint returns true the two sets in question are disjoint, that is if they do not have even a single element in common. The two sets s1 and s2 do not have any elements in common, hence true is returned in both the cases.
6. If we have two sets, s1 and s2, and we want to check if all the elements of s1 are present in s2 or not, we can use the function:
a) s2.issubset(s1)
b) s2.issuperset(s1)
c) s1.issuperset(s2)
d) s1.isset(s2)
