ProgrammingHardAmazon OA · Infosys SP

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 x = 1221, rev = 0
while (x > rev)
    rev = rev * 10 + x mod 10
    x = x / 10
end while
Print x, rev, (x == rev OR x == rev / 10)
  1. A. 12 12 true
  2. B. 1221 1221 true
  3. C. 1 122 false
  4. D. 122 1 false

Correct answer: A. 12 12 true

Only half the number is reversed: (x, rev) goes (122, 1) → (12, 12), and the loop stops since x is no longer greater than rev. 12 == 12, so it is a palindrome: 12 12 true.

More Pseudocode and Code Tracing questions

Exams that ask this topic