ProgrammingHardCognizant GenC Next · Accenture

What will be the output of the following pseudocode? (mod keeps the sign of the dividend; code/char convert between letters and codes)

What will be the output of the following pseudocode? (mod keeps the sign of the dividend; code/char convert between letters and codes)
String s = "DEF", r = ""
Integer shift = -5
for (each i from 0 to length(s) - 1)
    Integer k = ((code(s[i]) - code('A') + shift) mod 26 + 26) mod 26
    r = r + char(code('A') + k)
end for
Print r
  1. A. YZA
  2. B. IJK
  3. C. ABC
  4. D. XYZ

Correct answer: A. YZA

Shifting back 5 wraps past A: D (3) → −2 → 24 = Y, E → 25 = Z, F → 0 = A. The + 26 fixes the negative remainder, giving YZA.

More Pseudocode and Code Tracing questions

Exams that ask this topic