ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (tail-recursive factorial with an accumulator)?

What will be the output of the following pseudocode (tail-recursive factorial with an accumulator)?
Function factAcc(Integer n, Integer acc)
    if (n <= 1)
        return acc
    end if
    return factAcc(n - 1, acc * n)
End Function

Print factAcc(5, 1)
  1. A. 120
  2. B. 24
  3. C. 60
  4. D. 720

Correct answer: A. 120

The accumulator carries the running product forward: acc becomes 1*5=5, then 5*4=20, then 20*3=60, then 60*2=120, and n=1 returns that final acc, 120.

More Pseudocode and Code Tracing questions

Exams that ask this topic