What will the output of the following pseudocode be for i = 140? integer…
2024
What will the output of the following pseudocode be for i = 140?
integer fun(int i)
if ((i % 2) != 0)
return i;
else
return fun(fun(i = 1));
End function fun()- A.
1
- B.
2
- C.
3
- D.
0 (Zero)
Show answer & explanation
Correct answer: A
Concept: When tracing a recursive function guarded by an if/else base case, evaluate the innermost call first. If a call reassigns its own parameter before invoking a nested call, that reassigned value — not the original input — decides which branch the nested call takes, and whatever that nested call returns then becomes the argument passed to the call that encloses it.
Step-by-step trace for i = 140:
i = 140 is passed to fun(). Since 140 % 2 == 0, the condition (i % 2) != 0 is false, so the else branch executes.
The else branch evaluates fun(fun(i = 1)) from the inside out. The inner argument first performs the assignment i = 1, so the innermost call becomes fun(1) — the original value 140 plays no further part.
Inside that inner fun(1): 1 % 2 != 0 is true, so the if-branch runs and the call simply returns its own argument, giving back 1.
That returned value now becomes the argument to the outer call, so the outer call is also effectively fun(1).
Inside this outer fun(1), the same if-condition is true again, so it too returns its own argument.
The overall call fun(140) therefore evaluates to the value produced by this chain.
Cross-check: This collapse does not depend on the original input at all — for any even starting value (8, 2024, or 140), the else branch always reduces to fun(fun(i = 1)), which resolves through the same two odd-base-case returns. So every even input to this function produces the same output, and 140 is no exception.