OA. free
Free
Accenture Core Computer Science Core Computer Science Medium

For the next recursive function, what is the value of f(10)?

Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.

For the next recursive function, what is the value of f(10)?

int f(int x){
  if (x<=1)
    return 0
  else if (x%2==0
    return f(x+1)+x
  else
    return f(x-3)-x
}
Choose one option.
Show answer & explanation
Answer: D. Can't be determined

The function enters an infinite loop. When x is even, it calls f(x+1), which makes x odd, then f(x-3) is called, which eventually returns to an even number greater than the original. This cycle never terminates or reaches the base case (x≤1), so the function never returns and the value cannot be determined.

Step-by-step Derivation:
Trace f(10):

  • f(10): x=10 is even, so return f(11)+10
  • f(11): x=11 is odd, so return f(8)-11
  • f(8): x=8 is even, so return f(9)+8
  • f(9): x=9 is odd, so return f(6)-9
  • f(6): x=6 is even, so return f(7)+6
  • f(7): x=7 is odd, so return f(4)-7
  • f(4): x=4 is even, so return f(5)+4
  • f(5): x=5 is odd, so return f(2)-5
  • f(2): x=2 is even, so return f(3)+2
  • f(3): x=3 is odd, so return f(0)-3
  • f(0): x=0≤1, so return 0

Now unwinding: f(0)=0 → f(3)=0-3=-3 → f(2)=-3+2=-1 → f(5)=-1-5=-6 → f(4)=-6+4=-2 → f(7)=-2-7=-9 → f(6)=-9+6=-3 → f(9)=-3-9=-12 → f(8)=-12+8=-4 → f(11)=-4-11=-15 → f(10)=-15+10=-5

Actually, the function does terminate! Re-examining the trace shows it reaches the base case. The correct answer is B) -5.