Test Series - python

Test Number 73/108

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

odd=lambda x: bool(x%2)
numbers=[n for n in range(10)]
print(numbers)
n=list()
for i in numbers:
    if odd(i):
        continue
    else:
        break
A. [0, 2, 4, 6, 8, 10]
B. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
C. [1, 3, 5, 7, 9]
D. Error
Solution: The code shown above returns a new list containing whole numbers up to 10 (excluding 10). Hence the output of the code is: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].
Q: What will be the output of the following Python code?

f=lambda x:bool(x%2)
print(f(20), f(21))
A. False True
B. False False
C. True True
D. True False
Solution: The code shown above will return true if the given argument is an odd number, and false if the given argument is an even number. Since the arguments are 20 and 21 respectively, the output of this code is: False True.
Q: What will be the output of the following Python code?

import functools
l=[1,2,3,4]
print(functools.reduce(lambda x,y:x*y,l))
A. Error
B. 10
C. 24
D. No output
Solution: The code shown above returns the product of all the elements of the list. Hence the output is 1*2*3*4 = 24.
Q: What will be the output of the following Python code?

l=[1, -2, -3, 4, 5]
def f1(x):
    return x<2
m1=filter(f1, l)
print(list(m1))
A. [1, 4, 5 ]
B. Error
C. [-2, -3]
D. [1, -2, -3]
Solution: The code shown above returns only those elements from the list, which are less than 2. The functional programming tool used to achieve this operation is filter. Hence the output of the code is:[1, -2, -3].
Q: What will be the output of the following Python code?

l=[-2, 4]
m=map(lambda x:x*2, l)
print(m)
A. [-4, 16]
B. Address of m
C. Error
D. -4 16
Solution: The code shown above returns the address of m. Had we used the statement: print(list(m)), the output would have been: [-4, 16].
Q: What will be the output of the following Python code?

l=[1, -2, -3, 4, 5]
def f1(x):
    return x<-1
m1=map(f1, l)
print(list(m1))
A. [False, False, False, False, False]
B. [False, True, True, False, False]
C. [True, False, False, True, True]
D. [True, True, True, True, True]
Solution: This code shown returns a list which contains True if the corresponding element of the list is less than -1, and false if the corresponding element is greater than -1. Hence the output of the code shown above: [False, True, True, False, False].
Q: What will be the output of the following Python code?

l=[1, 2, 3, 4, 5]
m=map(lambda x:2**x, l)
print(list(m))
A. [1, 4, 9, 16, 25 ]
B. [2, 4, 8, 16, 32 ]
C. [1, 0, 1, 0, 1]
D. Error
Solution: The code shown above prints a list containing each element of the list as the power of two. That is, the output is: [2, 4, 8, 16, 32].
Q: What will be the output of the following Python code?

import functools
l=[1, 2, 3, 4, 5]
m=functools.reduce(lambda x, y:x if x>y else y, l)
print(m)
A. Error
B. Address of m
C. 1
D. 5
Solution: The code shown above can be used to find the maximum of the elements from the given list. In the above code, this operation is achieved by using the programming tool reduce. Hence the output of the code shown above is 5.
Q: What will be the output of the following Python code?

l=[n for n in range(5)]
f=lambda x:bool(x%2)
print(f(3), f(1))
for i in range(len(l)):
    if f(l[i]):
        del l[i]
        print(i)
A. True True 1 2 Error
B.  False False 1 2
C. True False 1 2 Error
D. False True 1 2
Solution: The code shown above prints true if the value entered as an argument is odd, else false is printed. Hence the output: True True. The error is due to the list index being out of range.
Q: What will be the output of the following Python code?

m=reduce(lambda x: x-3 in range(4, 10))
print(list(m))
A. [1, 2, 3, 4, 5, 6, 7]
B. No output
C. [1, 2, 3, 4, 5, 6]
D. Error
Solution: The code shown above will result in an error. This is because e have not imported functools. Further, ‘reduce’, as such is not defined. We should use functools.reduce to remove the error.
Q: Which of the following numbers will not be a part of the output list of the following Python code?

def sf(a):
    return a%3!=0 and a%5!=0
m=filter(sf, range(1, 31))
print(list(m))
A. 1
B. 29
C. 6
D. 10
Solution: The output list of the code shown above will not contain any element that is divisible by 3 or 5. Hence the number which is not present in the output list is 10. The output list: [1, 2, 4, 7, 8, 11, 13, 14, 16, 17, 19, 22, 23, 26, 28, 29]

You Have Score    /11