What will be the output of the following pseudocode when n = 3 ? Integer…
2024
What will be the output of the following pseudocode when n = 3 ?
Integer calc(Integer n)
if(n <= 0)
return 0;
if(n < 1)
return 1;
Print n
calc(n-1);
Print n
End function calcAnswer: C. 3 2 1 1 2 3 — Solution: calc(3) Prints 3 Calls calc(2) calc(2) Prints 2 Calls calc(1) calc(1) Prints 1 Calls calc(0) calc(0) Condition (n <= 0) becomes true Function…
- A.
1 2 3
- B.
3 2 1 0
- C.
3 2 1 1 2 3
- D.
Compiler error
Attempted by 139 students.
Show answer & explanation
Correct answer: C
Solution:
calc(3)
Prints 3
Calls calc(2)
calc(2)
Prints 2
Calls calc(1)
calc(1)
Prints 1
Calls calc(0)
calc(0)
Condition (n <= 0) becomes true
Function returns 0
No value is printed
Now recursion returns back:
calc(1) prints 1
calc(2) prints 2
calc(3) prints 3
Therefore, the final output is:
3 2 1 1 2 3
Loading lesson…