ProgrammingHardAccenture · Capgemini

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

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings)
String digits = "0123456789ABCDEF", r = ""
Integer n = 2024
while (n > 0)
    r = digits[n mod 16] + r
    n = n / 16
end while
Print r
  1. A. 8E7
  2. B. 7D8
  3. C. 7E8
  4. D. 7E9

Correct answer: C. 7E8

2024 = 7 × 256 + 14 × 16 + 8, so the hexadecimal digits are 7, E, 8: 7E8. Appending instead of prepending would give the reversed 8E7.

More Pseudocode and Code Tracing questions

Exams that ask this topic