This set of Tough Python 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
Explanation: SyntaxError, unbalanced parenthesis.
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
Explanation: Each element of x is converted into a list.
3. What will be the output of the following Python code?
x = [12, 34] print(len(list(map(len, x))))
a) 2
b) 1
c) error
d) none of the mentioned
View Answer
Explanation: TypeError, int has no len().
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
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
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
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
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
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
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
Explanation: map() is applied to the elements of the outer loop.
Sanfoundry Global Education & Learning Series – Python.
To practice all tough questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Programming Internship
- Apply for Python Internship
- Check Python Books
- Practice Programming MCQs
- Check Information Technology Books