ProgrammingHardProduct company OA · Infosys pattern

What will be the output of the following pseudocode?

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

Correct answer: D. 1 3 2 1

fun(3) first calls fun(1): that calls fun(−1) (nothing), prints 1, calls fun(0) (nothing). Back in fun(3): print 3, then fun(2): fun(0) nothing, print 2, fun(1) prints 1. Output: 1 3 2 1. Printing before the first recursive call (preorder style) gives 3 1 2 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic