Test Series - python

Test Number 52/108

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

>>> a={1,2,3}
>>> a.intersection_update({2,3,4,5})
>>> a
A. {2,3}
B. Error, duplicate item present in list
C. Error, no method called intersection_update for set data type
D. {1,4,5}
Solution: The method intersection_update returns a set which is an intersection of both the sets.
Q: What will be the output of the following Python code?

>>> a={1,2,3}
>>> b=a
>>> b.remove(3)
>>> a
A. {1,2,3}
B. Error, copying of sets isn’t allowed
C. {1,2}
D. Error, invalid syntax for remove
Solution: Any change made in b is reflected in a because b is an alias of a.
Q: What will be the output of the following Python code?

>>> a={1,2,3}
>>> b=a.copy()
>>> b.add(4)
>>> a
A. {1,2,3}
B. Error, invalid syntax for add
C. {1,2,3,4}
D. Error, copying of sets isn’t allowed
Solution: In the above piece of code, b is barely a copy and not an alias of a. Hence any change made in b isn’t reflected in a.
Q: What will be the output of the following Python code?

>>> a={1,2,3}
>>> b=a.add(4)
>>> b
A. 0
B. {1,2,3,4}
C. {1,2,3}
D. Nothing is printed
Solution: The method add returns nothing, hence nothing is printed.
Q: What will be the output of the following Python code?

>>> a={1,2,3}
>>> b=frozenset([3,4,5])
>>> a-b
A. {1,2}
B. Error as difference between a set and frozenset can’t be found out
C. Error as unsupported operand type for set data type
D. frozenset({1,2})
Solution: operator gives the set of elements in set a but not in set b.
Q: What will be the output of the following Python code?

>>> a={5,6,7}
>>> sum(a,5)
A. 5
B. 23
C. 18
D. Invalid syntax for sum method, too many arguments
Solution: The second parameter is the start value for the sum of elements in set a. Thus, sum(a,5) = 5+(5+6+7)=23.
Q: What will be the output of the following Python code?

>>> a={1,2,3}
>>> {x*2 for x in a|{4,5}}
A. {2,4,6}
B. Error, set comprehensions aren’t allowed
C. {8, 2, 10, 4, 6}
D. {8,10}
Solution: Set comprehensions are allowed.
Q: What will be the output of the following Python code?

>>> a={5,6,7,8}
>>> b={7,8,9,10}
>>> len(a+b)
A. 8
B. Error, unsupported operand ‘+’ for sets
C. 6
D. Nothing is displayed
Solution: Duplicate elements in a+b is eliminated and the length of a+b is computed.
Q: What will be the output of the following Python code?

a={1,2,3}
b={1,2,3}
c=a.issubset(b)
print(c)
A. True
B. Error, no method called issubset() exists
C. Syntax error for issubset() method
D. False
Solution: The method issubset() returns True if b is a proper subset of a.
Q: Is the following Python code valid?

a={1,2,3}
b={1,2,3,4}
c=a.issuperset(b)
print(c)
A. False
B. True
C. Syntax error for issuperset() method
D. Error, no method called issuperset() exists
Solution: The method issubset() returns True if b is a proper subset of a.

You Have Score    /10