ProgrammingHardCapgemini · Infosys 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 > 0)
        fun(n - 1)
        Print n
        fun(n - 1)
    end if
End Function

fun(3)
  1. A. 1 1 2 1 1 3
  2. B. 1 2 3 2 1
  3. C. 3 2 1 1 2 3
  4. D. 1 2 1 3 1 2 1

Correct answer: D. 1 2 1 3 1 2 1

fun(1) prints 1. fun(2) prints fun(1), 2, fun(1) = 1 2 1. fun(3) prints fun(2), 3, fun(2) = 1 2 1 3 1 2 1, which is 2³ - 1 = 7 numbers (the Tower of Hanoi pattern).

More Pseudocode and Code Tracing questions

Exams that ask this topic