ProgrammingVery hardTCS NQT · Infosys · Accenture

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)
        g(n - 1)
        Print n
        g(n - 2)
    end if
end function
g(3)
  1. A. 1 2 3
  2. B. 3 2 1 1
  3. C. 1 2 3 1
  4. D. 1 2 1 3

Correct answer: C. 1 2 3 1

g(3) = g(2), print 3, g(1). g(2) = g(1), print 2, g(0); g(1) prints 1. So the output is 1 2 3 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic