ProgrammingModerateTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function fun(Integer n)
    if (n < 3)
        return n
    end if
    return fun(n - 1) + 2 * fun(n - 3)
End Function

Print fun(6)
  1. A. 8
  2. B. 16
  3. C. 10
  4. D. 12

Correct answer: D. 12

Build it up from the bottom: fun(0) = 0, fun(1) = 1, fun(2) = 2, fun(3) = 2 + 0 = 2, fun(4) = 2 + 2 = 4, fun(5) = 4 + 4 = 8, fun(6) = 8 + 2 × 2 = 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic