Test Series - python

Test Number 53/108

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

s=set()
type(s)
A. <’set’>
B. 
C. set
D. class set
Solution: When we find the type of a set, the output returned is: .
Q: The following Python code results in an error.

s={2, 3, 4, [5, 6]}
A. True
B. False
C. none
D. ...
Solution: The set data type makes use of a principle known as hashing. This means that each item in the set should be hashable. Hashable in this context means immutable. List is mutable and hence the line of code shown above will result in an error.
Q: Set makes use of __________
Dictionary makes use of ____________
A. keys, keys
B. key values, keys
C. keys, key values
D. key values, key values
Solution: Set makes use of keys.
Dictionary makes use of key values.
Q: Which of the following lines of code will result in an error?
A. s={abs}
B. s={4, ‘abc’, (1,2)}
C. s={2, 2.2, 3, ‘xyz’}
D. s={san}
Solution: The line: s={san} will result in an error because ‘san’ is not defined. The line s={abs} does not result in an error because abs is a built-in function. The other sets shown do not result in an error because all the items are hashable.
Q: What will be the output of the following Python code?

s={2, 5, 6, 6, 7}
s
A. {2, 5, 7}
B. {2, 5, 6, 7}
C. {2, 5, 6, 6, 7}
D. Error
Solution: Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Therefore the output is: {2, 5, 6, 7}
Q: Input order is preserved in sets.
A. True
B. False
C. none
D. ...
Solution: The input order in sets is not maintained. This is demonstrated by the code shown below:
>>> s={2, 6, 8, 1, 5}
>>> s
{8, 1, 2, 5, 6}
Q: Write a list comprehension for number and its cube for:

l=[1, 2, 3, 4, 5, 6, 7, 8, 9]
A. [x**3 for x in l]
B. [x^3 for x in l]
C. [x**3 in l]
D. [x^3 in l]
Solution: The list comprehension to print a list of cube of the numbers for the given list is: [x**3 for x in l].
Q: What will be the output of the following Python code?

s={1, 2, 3}
s.update(4)
s
A. {1, 2, 3, 4}
B. {1, 2, 4, 3}
C. {4, 1, 2, 3}
D. Error
Solution: The code shown above will result in an error because the argument given to the function update should necessarily be an iterable. Hence if we write this function as: s.update([4]), there will be no error.
Q: Which of the following functions cannot be used on heterogeneous sets?
A. pop
B. remove
C. update
D. sum
Solution: The functions sum, min and max cannot be used on mixed type (heterogeneous) sets. The functions pop, remove, update etc can be used on homogenous as well as heterogeneous sets. An example of heterogeneous sets is: {‘abc’, 4, (1, 2)}
Q: What will be the output of the following Python code?

s={4>3, 0, 3-3}
all(s)
any(s)
A. True False
B. False True
C. True True
D. False False
Solution: The function all returns true only if all the conditions given are true. But in the example shown above, we have 0 as a value. Hence false is returned. Similarly, any returns true if any one condition is true. Since the condition 4>3 is true, true is returned.

You Have Score    /10