ProgrammingVery hardGoldman Sachs OA · TCS Digital

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function G(Integer n)
    if (n == 0)
        return 0
    end if
    return n - G(G(n - 1))
End Function
Print G(10), G(11)
  1. A. 6 7
  2. B. 5 6
  3. C. 6 6
  4. D. 7 8

Correct answer: A. 6 7

Build the table: G(0..11) = 0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7. For example G(11) = 11 − G(G(10)) = 11 − G(6) = 11 − 4 = 7. Output 6 7.

More Pseudocode and Code Tracing questions

Exams that ask this topic