PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 55
Q422 - Functions
What is the expected output of the following code?
- def func1(x):
- return str(x)
- def func2(x):
- return str(2 * x)
- print(func1(1) + func2(2))
-
A
14 -
B
5 -
C
The code is erroneous.
-
D
3
Reveal correct answer
Correct answer: A
Explanation
Topics: def return str() string concatenation
Try it yourself:
- def func1(x):
- return str(x)
- def func2(x):
- return str(2 * x)
- print(func1(1) + func2(2)) # 14
Explanation:
Both functions will return a string
func1() the string '1'
and func2() the string '4'
The string concatenation '1' + '4' adds up to '14'
Q422 (Please refer to this number, if you want to write me about this question.)
A. The expected output of the code is '14' because the function func1(1) returns '1' as a string and the function func2(2) returns '4' as a string. When concatenated, '1' and '4' result in '14'.
B. The output '5' is incorrect because the code concatenates the string representations of the return values of func1(1) and func2(2), which are '1' and '4', respectively. Therefore, the correct output is '14', not '5'.
C. The code is not erroneous as it defines two functions, func1 and func2, correctly and prints the concatenated result of calling these functions with arguments 1 and 2, respectively. The output '14' is the expected result of the code execution.
D. The output '3' is incorrect because the code concatenates the string representations of the return values of func1(1) and func2(2), which are '1' and '4', respectively. Therefore, the correct output is '14', not '3'.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
