Test Series - python

Test Number 59/108

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

>>>import collections
>>> a=collections.Counter([1,1,2,3,3,4,4,4])
>>> a
A. {1,2,3,4}
B. Counter({4, 1, 3, 2})
C. Counter({4: 3, 1: 2, 3: 2, 2: 1})
D. {4: 3, 1: 2, 3: 2, 2: 1}
Solution: The statement a=collections.OrderedDict() generates a dictionary with the number as the key and the count of times the number appears as the value.
Q: What will be the output of the following Python code snippet?

>>>import collections
>>> b=collections.Counter([2,2,3,4,4,4])
>>> b.most_common(1)
A. Counter({4: 3, 2: 2, 3: 1})
B. {3:1}
C. {4:3}
D. [(4, 3)]
Solution: The most_common() method returns the n number key-value pairs where the value is the most recurring.
Q: What will be the output of the following Python code snippet?

>>>import collections
>>> b=collections.Counter([2,2,3,4,4,4])
>>> b.most_common(1)
A. Counter({4: 3, 2: 2, 3: 1})
B. {3:1}
C. {4:3}
D. [(4, 3)]
Solution: The most_common() method returns the n number key-value pairs where the value is the most recurring.
Q: What will be the output of the following Python code snippet?

>>> import collections
>>> a=collections.Counter([2,2,3,3,3,4])
>>> b=collections.Counter([2,2,3,4,4])
>>> a|b
A. Counter({3: 3, 2: 2, 4: 2})
B. Counter({2: 2, 3: 1, 4: 1})
C. Counter({3: 2})
D. Counter({4: 1})
Solution: a|b returns the pair of keys and the highest recurring value.
Q: What will be the output of the following Python code snippet?

>>> import collections
>>> a=collections.Counter([3,3,4,5])
>>> b=collections.Counter([3,4,4,5,5,5])
>>> a&b
A. Counter({3: 12, 4: 1, 5: 1})
B. Counter({3: 1, 4: 1, 5: 1})
C. Counter({4: 2})
D. Counter({5: 1})
Solution: a&b returns the pair of keys and the lowest recurring value.
Q: What will be the output of the following Python code?

>>> a=dict()
>>> a[1]
A. An exception is thrown since the dictionary is empty
B. ‘ ‘
C. 1
D. 0
Solution: The values of a dictionary can be accessed through the keys only if the keys exist in the dictionary.
Q: What will be the output of the following Python code?

>>> import collections
>>> a=dict()
>>> a=collections.defaultdict(int)
>>> a[1]
A. 1
B. 0
C. An exception is thrown
D. ‘ ‘
Solution: The statement a=collections.defaultdict(int) gives the default value of 0
(since int data type is given within the parenthesis) even if the keys don’t exist in the dictionary.
Q: What will be the output of the following Python code?

>>> import collections
>>> b=dict()
>>> b=collections.defaultdict(lambda: 7)
>>> b[4]
A. An exception is thrown
B. 4
C. 0
D. 7
Solution: The statement a=collections.defaultdict(lambda: x) gives the default value of x even if the keys don’t exist in the dictionary.
Q: What will be the output of the following Python code?

>>> import collections
>>> a=collections.OrderedDict((str(x),x) for x in range(3))
>>> a
A. {‘2’:2, ‘0’:0, ‘1’:1}
B. OrderedDict([(‘0’, 0), (‘1’, 1), (‘2’, 2)])
C. An exception is thrown
D. ‘ ‘
Solution: The line of code a=collections.OrderedDict() generates a dictionary satisfying the conditions given within the parenthesis and in an ascending order of the keys.
Q: Which of the following functions is a built-in function in python?
A. seed()
B. sqrt()
C. factorial()
D. print()
Solution: The function seed is a function which is present in the random module. The functions sqrt and factorial are a part of the math module. The print function is a built-in function which prints a value directly to the system output.
Q: What will be the output of the following Python expression?

round(4.576)
A. 4.5
B. 5
C. 4
D. 4.6
Solution: This is a built-in function which rounds a number to give precision in decimal digits. In the above case, since the number of decimal places has not been specified, the decimal number is rounded off to a whole number. Hence the output will be 5.
Q: The function pow(x,y,z) is evaluated as:
A. (x**y)**z
B. (x**y) / z
C. (x**y) % z
D. (x**y)*z
Solution: The built-in function pow() can accept two or three arguments. When it takes in two arguments, they are evaluated as x**y. When it takes in three arguments, they are evaluated as (x**y)%z.
Q: What will be the output of the following Python function?

all([2,4,0,6])
A. Error
B. True
C. False
D. 0
Solution: The function all returns false if any one of the elements of the iterable is zero and true if all the elements of the iterable are non zero. Hence the output of this function will be false.
Q: What will be the output of the following Python expression?

round(4.5676,2)?
A. 4.5
B. 4.6
C. 4.57
D. 4.56
Solution: The function round is used to round off the given decimal number to the specified decimal places. In this case, the number should be rounded off to two decimal places. Hence the output will be 4.57.

You Have Score    /14