Python Questions and Answers – Functional Programming Tools

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Functional Programming Tools”.

1. 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
View Answer

Answer: b
Explanation: 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].
advertisement
advertisement

2. 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
View Answer

Answer: a
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will be the output of the following Python code?

advertisement
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
View Answer

Answer: c
Explanation: The code shown above returns the product of all the elements of the list. Hence the output is 1*2*3*4 = 24.
advertisement

4. 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]
View Answer

Answer: d
Explanation: 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].

5. 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
View Answer
Answer: b
Explanation: The code shown above returns the address of m. Had we used the statement: print(list(m)), the output would have been: [-4, 16].
 
 

6. 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]
View Answer

Answer: b
Explanation: 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].

7. 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
View Answer

Answer: b
Explanation: 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].

8. 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
View Answer

Answer: d
Explanation: 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.

9. 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
View Answer
Answer: a
Explanation: 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.
 
 

10. 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
View Answer

Answer: b
Explanation: 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.

11. 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
View Answer

Answer: d
Explanation: 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]

12. The single line equivalent of the following Python code?

l=[1, 2, 3, 4, 5]
def f1(x):
    return x<0
m1=filter(f1, l)
print(list(m1))

a) filter(lambda x:x<0, l)
b) filter(lambda x, y: x<0, l)
c) filter(reduce x<0, l)
d) reduce(x: x<0, l)
View Answer

Answer: a
Explanation: The code shown above returns a new list containing only those elements from list l, which are less than 0. Since there are no such elements in the list l, the output of this code is: []. The single line equivalent of this code is filter(lambda x:x<0, l).

13. What will be the output of the following Python code?

list(map((lambda x:x^2), range(10)))

a) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
b) Error
c) [2, 3, 0, 1, 6, 7, 4, 5, 10, 11]
d) No output
View Answer

Answer: c
Explanation: The line of code shown above returns a list of each number from 1 to 10, after an XOR operation is performed on each of these numbers with 2. Hence the output of this code is: [2, 3, 0, 1, 6, 7, 4, 5, 10, 11]

14. What will be the output of the following Python code?

list(map((lambda x:x**2), filter((lambda x:x%2==0), range(10))))

a) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b) [0, 4, 16, 36, 64]
c) Error
d) No output
View Answer

Answer: b
Explanation: The output list will contain each number up to 10 raised to 2, except odd numbers, that is, 1, 3, 5, 9. Hence the output of the code is: [0, 4, 16, 36, 64].

15. The output of the following codes are the same.

[x**2 for x in range(10)]
list(map((lambda x:x**2), range(10)))

a) True
b) False
View Answer

Answer: a
Explanation: Both of the codes shown above print each whole number up to 10, raised to the power 2. Hence the output of both of these codes is: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]. Therefore, the statement is true.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.