ProgrammingHardCapgemini · Infosys pattern

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Function rec(Integer n, Integer depth)
    Print depth, n
    if (n > 1)
        rec(n / 2, depth + 1)
    end if
End Function

rec(10, 0)
  1. A. 0 10 1 5 2 2 3 1
  2. B. 0 10 1 5 2 2
  3. C. 0 10 1 5 2 2 3 1 4 0
  4. D. 0 5 1 2 2 1

Correct answer: A. 0 10 1 5 2 2 3 1

Each call halves n (integer division) and increases depth: (0,10) -> (1,5) -> (2,2) -> (3,1). Since n=1 is not > 1, the recursion stops after printing (3, 1).

More Pseudocode and Code Tracing questions

Exams that ask this topic