ProgrammingHardTCS NQT · Accenture

The procedure below reverses the digits of N = 120450 (trailing zeros disappear in the numeric result). What is printed?

The procedure below reverses the digits of N = 120450 (trailing zeros disappear in the numeric result). What is printed?
rev = 0; m = N
while m > 0
    rev = rev * 10 + (m mod 10)
    m = m div 10
end while
Print rev
  1. A. 120450
  2. B. 120
  3. C. 540210
  4. D. 54021

Correct answer: D. 54021

Reversing the digits gives 054021, which as a number is 54021, so the trailing zero of the original becomes a leading zero that disappears. Printing the number unchanged, using the wrong place value, and shifting the result by a factor of ten each give different values.

More Pseudocode and Code Tracing questions

Exams that ask this topic