PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 28
Q405 - Functions
What is the expected output of the following code?
- def func1(param):
- return param
- def func2(param):
- return param * 2
- def func3(param):
- return param + 3
- print(func1(func2(func3(1))))
-
A
6 -
B
1 -
C
8 -
D
3
Reveal correct answer
Correct answer: C
Explanation
Topics: def return
Try it yourself:
- def func1(param):
- return param # 8 -> 8
- def func2(param):
- return param * 2 # 4 * 2 -> 8
- def func3(param):
- return param + 3 # 1 + 3 -> 4
- print(func1(func2(func3(1)))) # 8
Explanation:
func3() gets called with 1 and returns 4
func2() gets called with 4 and returns 8
func1() gets called with 8 and returns 8
Q405 (Please refer to this number, if you want to write me about this question.)
A. This choice is incorrect because it does not consider the addition by 3 in func3 and the multiplication by 2 in func2. The final output is not 6, but 8.
B. This choice is incorrect because it does not consider the multiplication by 2 in func2 and the addition by 3 in func3. The final output is not 1, but 8.
C. The code first calls func3(1), which returns 1 + 3 = 4. Then, the result of func3(1) is passed to func2, which returns 4 * 2 = 8. Finally, the result of func2(func3(1)) is passed to func1, which simply returns the value passed to it. Therefore, the expected output is 8.
D. This choice is incorrect as it does not account for the multiplication by 2 in func2. The final output is not 3, but 8.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
