Test Series - python

Test Number 60/108

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

count={}
count[(1,2,4)] = 5
count[(4,2,1)] = 7
count[(1,2)] = 6
count[(4,2,1)] = 2
tot = 0
for i in count:
    tot=tot+count[i]
print(len(count)+tot)
A. 25
B. 17
C. 16
D. Tuples can’t be made keys of a dictionary
Solution: Tuples can be made keys of a dictionary. Length of the dictionary is 3 as the value of the key (4,2,1) is modified to 2. The value of the variable tot is 5+6+2=13.
Q: What will be the output of the following Python code?

a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
A. [2,3,4]
B. 3
C. 2
D. An exception is thrown
Solution: Now, a={1:[2,3,4],2:1} . a[1][1] refers to second element having key 1.
Q: What will be the output of the following Python code?

>>> a={i: i*i for i in range(6)}
>>> a
A. Dictionary comprehension doesn’t exist
B. {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
C. {0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
D. {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Solution: Dictionary comprehension is implemented in the above piece of code.
Q: What will be the output of the following Python code?

>>> a={}
>>> a.fromkeys([1,2,3],"check")
A. Syntax error
B. {1:”check”,2:”check”,3:”check”}
C. “check”
D. {1:None,2:None,3:None}
Solution: The dictionary takes values of keys from the list and initializes it to the default value (value given in the second parameter). Execute in Python shell to verify.
Q: What will be the output of the following Python code?

>>> b={}
>>> all(b)
A. { }
B. False
C. True
D. An exception is thrown
Solution: Function all() returns True if all keys of the dictionary are true or if the dictionary is empty.
Q: If b is a dictionary, what does any(b) do?
A. Returns True if any key of the dictionary is true
B. Returns False if dictionary is empty
C. Returns True if all keys of the dictionary are true
D. Method any() doesn’t exist for dictionary
Solution: Method any() returns True if any key of the dictionary is true and False if the dictionary is empty.
Q: What will be the output of the following Python code?

>>> a={"a":1,"b":2,"c":3}
>>> b=dict(zip(a.values(),a.keys()))
>>> b
A. {‘a’: 1, ‘b’: 2, ‘c’: 3}
B. An exception is thrown
C. {‘a’: ‘b’: ‘c’: }
D. {1: ‘a’, 2: ‘b’, 3: ‘c’}
Solution: The above piece of code inverts the key-value pairs in the dictionary.
Q: What will be the output of the following Python function?

any([2>8, 4>2, 1>2])
A. Error
B. True
C. False
D. 4>2
Solution: The built-in function any() returns true if any or more of the elements of the iterable is true (non zero), If all the elements are zero, it returns false.
Q: What will be the output of the following Python function?

import math
abs(math.sqrt(25))
A. Error
B. -5
C. 5
D. 5.0
Solution: The abs() function prints the absolute value of the argument passed. For example: abs(-5)=5. Hence, in this case we get abs(5.0)=5.0.
Q: What will be the output of the following Python function?

sum(2,4,6)
sum([1,2,3])
A. Error, 6
B. 12, Error
C. 12, 6
D. Error, Error
Solution: The first function will result in an error because the function sum() is used to find the sum of iterable numbers. Hence the outcomes will be Error and 6 respectively.
Q: What will be the output of the following Python function?

all(3,0,4.2)
A. True
B. False
C. Error
D. 0
Solution: The function all() returns ‘True’ if any one or more of the elements of the iterable are non zero. In the above case, the values are not iterable, hence an error is thrown.
Q: What will be the output of the following Python function?

min(max(False,-3,-4), 2,7)
A. False
B. 2
C. -3
D. -4
Solution: The function max() is being used to find the maximum value from among -3, -4 and false. Since false amounts to the value zero, hence we are left with min(0, 2, 7) Hence the output is 0 (false).

You Have Score    /12