ProgrammingModerateCapgemini · 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.)
Integer n = 4521, r = 0
while (n > 0)
    r = r * 10 + n mod 10
    n = n / 10
end while
Print r
  1. A. 1245
  2. B. 4521
  3. C. 12
  4. D. 1254

Correct answer: D. 1254

Each pass takes the last digit (n mod 10) and appends it to r, then drops it from n. The digits come off as 1, 2, 5, 4, so r = 1254, the reverse of the number.

More Pseudocode and Code Tracing questions

Exams that ask this topic