Python Questions and Answers – Mapping Functions – 2

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

1. 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: c
Explanation: SyntaxError, unbalanced parenthesis.
advertisement
advertisement

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

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

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

Answer: c
Explanation: Each element of x is converted into a list.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
x = [12, 34]
print(len(list(map(len, x))))

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

Answer: c
Explanation: TypeError, int has no len().
advertisement

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

x = [12, 34]
print(len(list(map(int, x))))

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

Answer: a
Explanation: list(map()) returns a list of two items in this example.

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

x = [12, 34]
print(len(''.join(list(map(int, x)))))

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

Answer: c
Explanation: Cannot perform join on a list of ints.

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

x = [12, 34]
print(len(''.join(list(map(str, x)))))

a) 4
b) 5
c) 6
d) error
View Answer

Answer: a
Explanation: Each number is mapped into a string of length 2.

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

x = [12, 34]
print(len(' '.join(list(map(int, x)))))

a) 4
b) 5
c) 6
d) error
View Answer

Answer: d
Explanation: TypeError. Execute in shell to verify.

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

x = [12.1, 34.0]
print(len(' '.join(list(map(str, x)))))

a) 6
b) 8
c) 9
d) error
View Answer

Answer: c
Explanation: The floating point numbers are converted to strings and joined with a space between them.

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

x = [12.1, 34.0]
print(' '.join(list(map(str, x))))

a) 12 1 34 0
b) 12.1 34
c) 121 340
d) 12.1 34.0
View Answer

Answer: d
Explanation: str(ab.c) is ‘ab.c’.

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

x = [[0], [1]]
print(len(' '.join(list(map(str, x)))))

a) 2
b) 3
c) 7
d) 8
View Answer

Answer: c
Explanation: map() is applied to the elements of the outer loop.

Sanfoundry Global Education & Learning Series – Python.

To practice all tough 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.