Test Series - python

Test Number 51/108

Q: What will be the output of the following Python code?

>>> a={5,6,7,8}
>>> b={7,8,10,11}
>>> a^b
A. {5,6,7,8,10,11}
B. {7,8}
C. Error as unsupported operand type of set data type
D. {5,6,10,11}
Solution: ^ operator returns a set of elements in set A or set B, but not in both (symmetric difference).
Q: What will be the output of the following Python code?

>>> s={5,6}
>>> s*3
A. Error as unsupported operand type for set data type
B. {5,6,5,6,5,6}
C. {5,6}
D. Error as multiplication creates duplicate elements which isn’t allowed
Solution: The multiplication operator isn’t valid for the set data type.
Q: What will be the output of the following Python code?

>>> a={5,6,7,8}
>>> b={7,5,6,8}
>>> a==b
A. True
B. False
C. none
D. ..
Solution: It is possible to compare two sets and the order of elements in both the sets doesn’t matter if the values of the elements are the same.
Q: What will be the output of the following Python code?

>>> a={3,4,5}
>>> b={5,6,7}
>>> a|b
A. Invalid operation
B. {3, 4, 5, 6, 7}
C. {5}
D. {3,4,6,7}
Solution: The operation in the above piece of code is union operation. This operation produces a set of elements in both set a and set b.
Q: Is the following Python code valid?

a={3,4,{7,5}}
print(a[2][0])
A. Yes, 7 is printed
B. Error, elements of a set can’t be printed
C. Error, subsets aren’t allowed
D. Yes, {7,5} is printed
Solution: In python, elements of a set must not be mutable and sets are mutable. Thus, subsets can’t exist.
Q: Which of these about a frozenset is not true?
A. Mutable data type
B. Allows duplicate values
C. Data type with unordered values
D. Immutable data type
Solution: A frozenset is an immutable data type.
Q: What is the syntax of the following Python code?

>>> a=frozenset(set([5,6,7]))
>>> a
A. {5,6,7}
B. frozenset({5,6,7})
C. Error, not possible to convert set into frozenset
D. Syntax error
Solution: The above piece of code is the correct syntax for creating a frozenset.
Q: Is the following Python code valid?

>>> a=frozenset([5,6,7])
>>> a
>>> a.add(5)
A. Yes, now a is {5,5,6,7}
B. No, frozen set is immutable
C. No, invalid syntax for add method
D. Yes, now a is {5,6,7}
Solution: Since a frozen set is immutable, add method doesn’t exist for frozen method.
Q: Set members must not be hashable.
A. True
B. False
C. non
D. ...
Solution: Set members must always be hashable.
Q: What will be the output of the following Python code?

>>> a={3,4,5}
>>> a.update([1,2,3])
>>> a
A. Error, no method called update for set data type
B. {1, 2, 3, 4, 5}
C. Error, list can’t be added to set
D. Error, duplicate item present in list
Solution: The method update adds elements to a set.

You Have Score    /10