ProgrammingModerateTCS NQT · Accenture

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Procedure f(Integer n)
    if (n == 0)
        return
    end if
    Print n
    f(n - 1)
    Print n
End Procedure
f(4)
  1. A. 4 3 2 1 4 3 2 1
  2. B. 4 3 2 1 1 2 3 4
  3. C. 1 2 3 4 4 3 2 1
  4. D. 4 3 2 1 0 1 2 3 4

Correct answer: B. 4 3 2 1 1 2 3 4

Each call prints n before and after the recursive call, so the numbers go down and then come back up: 4 3 2 1 1 2 3 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic