Python Multiple Choice Questions – Mapping Functions

This set of Python Interview Questions & Answers focuses on “Mapping Functions”.

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

elements = [0, 1, 2]
def incr(x):
    return x+1
print(list(map(elements, incr)))

a) [1, 2, 3]
b) [0, 1, 2]
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: The list should be the second parameter to the mapping function.
advertisement
advertisement

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

elements = [0, 1, 2]
def incr(x):
    return x+1
print(list(map(incr, elements)))

a) [1, 2, 3]
b) [0, 1, 2]
c) error
d) none of the mentioned
View Answer

Answer: a
Explanation: Each element of the list is incremented.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
x = ['ab', 'cd']
print(list(map(upper, x)))

a) [‘AB’, ‘CD’]
b) [‘ab’, ‘cd’]
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: A NameError occurs because upper is a class method.
advertisement

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

def to_upper(k):
    return k.upper()
x = ['ab', 'cd']
print(list(map(upper, x)))

a) [‘AB’, ‘CD’]
b) [‘ab’, ‘cd’]
c) none of the mentioned
d) error
View Answer

Answer: d
Explanation: A NameError occurs because upper is a class method.

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

def to_upper(k):
    return k.upper()
x = ['ab', 'cd']
print(list(map(to_upper, x)))

a) [‘AB’, ‘CD’]
b) [‘ab’, ‘cd’]
c) none of the mentioned
d) error
View Answer

Answer: a
Explanation: Each element of the list is converted to uppercase.

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

def to_upper(k):
    k.upper()
x = ['ab', 'cd']
print(list(map(to_upper, x)))

a) [‘AB’, ‘CD’]
b) [‘ab’, ‘cd’]
c) none of the mentioned
d) error
View Answer

Answer: c
Explanation: A list of Nones is printed as to_upper() returns None.

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

x = ['ab', 'cd']
print(map(len, x))

a) [‘ab’, ‘cd’]
b) [2, 2]
c) [‘2’, ‘2’]
d) none of the mentioned
View Answer

Answer: d
Explanation: A map object is generated by map(). We must convert this to a list to be able to print it in a human readable form.

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

x = ['ab', 'cd']
print(list(map(len, x)))

a) [‘ab’, ‘cd’]
b) [2, 2]
c) [‘2’, ‘2’]
d) none of the mentioned
View Answer

Answer: b
Explanation: The length of each string is 2.

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

x = ['ab', 'cd']
print(len(map(list, x)))

a) [2, 2]
b) 2
c) 4
d) none of the mentioned
View Answer

Answer: d
Explanation: A TypeError occurs as map has no len().

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

x = ['ab', 'cd']
print(len(list(map(list, x))))

a) 2
b) 4
c) error
d) none of the mentioned
View Answer

Answer: a
Explanation: The outer list has two lists in it. So it’s length is 2.

More MCQs on Python Mapping Functions:

Sanfoundry Global Education & Learning Series – Python.

To practice all interview questions on 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.