ProgrammingHardAccenture · Capgemini

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)
Procedure show(Integer n)
    if (n == 0)
        return
    end if
    if (n mod 2 == 1)
        Print n mod 10
    end if
    show(n / 10)
    if (n mod 2 == 0)
        Print n mod 10
    end if
End Procedure
show(4736)
  1. A. 6 3 7 4
  2. B. 4 7 3 6
  3. C. 3 7 6 4
  4. D. 3 7 4 6

Correct answer: D. 3 7 4 6

4736 is even, so its digit 6 is printed after the recursion. 473 and 47 are odd and print 3 and 7 on the way down; 4 is even and prints after show(0). Order: 3 7 4 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic