Test Series - python

Test Number 55/108

Q: 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)}
Solution: 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)}.
Q: The ____________ function removes the first element of a set and the last element of a list.
A. remove
B. pop
C. discard
D. dispose
Solution: The function pop removes the first element when used on a set and the last element when used to a list.
Q: 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
Solution: 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).
Q: 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}
Solution: 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}.
Q: 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
Solution: 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.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.
Q: 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)
Solution: Since we are checking whether all the elements present in the set s1 are present in the set s2. This means that s1 is the subset and s1 is the superset. Hence the function to be used is: s2.issuperset(s1). This operation can also be performed by the function: s1.issubset(s2).
Q: What will be the output of the following Python code?

s1={1, 2, 3, 8}
s2={3, 4, 5, 6}
s1|s2
s1.union(s2)
A. {3} {1, 2, 3, 4, 5, 6, 8}
B. {1, 2, 4, 5, 6, 8} {1, 2, 4, 5, 6, 8}
C. {3} {3}
D. {1, 2, 3, 4, 5, 6, 8} {1, 2, 3, 4, 5, 6, 8}
Solution: The function s1|s2 as well as the function s1.union(s2) returns a union of the two sets s1 and s2. Hence the output of both of these functions is: {1, 2, 3, 4, 5, 6, 8}.
Q: What will be the output of the following Python code, if s1= {1, 2, 3}?

s1.issubset(s1)
A. True
B. Error
C. No output
D. False
Solution: Every set is a subset of itself and hence the output of this line of code is true.

You Have Score    /8