Test Series - python

Test Number 54/108

Q: Which of the following functions will return the symmetric difference between two sets, x and y?
A. x | y
B. x ^ y
C. x & y
D. x – y
Solution: The function x ^ y returns the symmetric difference between the two sets x and y. This is basically an XOR operation being performed on the two sets.
Q: What will be the output of the following Python code snippet?

s=set([1, 2, 3])
s.union([4, 5])
s|([4, 5])
A. {1, 2, 3, 4, 5} {1, 2, 3, 4, 5}
B.  Error {1, 2, 3, 4, 5}
C.  {1, 2, 3, 4, 5} Error
D.  Error Error
Solution: The first function in the code shown above returns the set {1, 2, 3, 4, 5}. This is because the method of the function union allows any iterable. However the second function results in an error because of unsupported data type, that is list and set.
Q: What will be the output of the following Python code snippet?

{a**2 for a in range(4)}
A. {1, 4, 9, 16}
B. {0, 1, 4, 9, 16}
C. Error
D. {0, 1, 4, 9}
Solution: The code shown above returns a set containing the square of values in the range 0-3, that is 0, 1, 2 and 3. Hence the output of this line of code is: {0, 1, 4, 9}.
Q: The output of the following code is: class<’set’>.

type({})
A. True
B. False
C. none
D. ..
Solution: The output of the line of code shown above is: class<’dict’>. This is because {} represents an empty dictionary, whereas set() initializes an empty set. Hence the statement is false.
Q: What will be the output of the following Python code snippet?

a=[1, 4, 3, 5, 2]
b=[3, 1, 5, 2, 4]
a==b
set(a)==set(b)
A.  True False
B. False False
C.  False True
D.  True True
Solution: In the code shown above, when we check the equality of the two lists, a and b, we get the output false. This is because of the difference in the order of elements of the two lists. However, when these lists are converted to sets and checked for equality, the output is true. This is known as order-neutral equality. Two sets are said to be equal if and only if they contain exactly the same elements, regardless of order.

You Have Score    /5