ProgrammingHardCognizant GenC Next · Infosys SP

What will be the output of the following pseudocode? (code(c) gives a character's code; 'A' to 'Z' have consecutive codes)

What will be the output of the following pseudocode? (code(c) gives a character's code; 'A' to 'Z' have consecutive codes)
String a = "AD", b = "BC"
Integer sa = 0, sb = 0
for (each i from 0 to 1)
    sa = sa + code(a[i])
    sb = sb + code(b[i])
end for
if (sa == sb)
    Print "ANAGRAM"
else
    Print "DIFFERENT"
end if
  1. A. DIFFERENT
  2. B. ANAGRAM
  3. C. ANAGRAMDIFFERENT
  4. D. Nothing is printed

Correct answer: B. ANAGRAM

The test only compares the sums of the character codes. Since A + D = B + C (codes are consecutive: 65 + 68 = 66 + 67), it prints ANAGRAM even though the words are not anagrams. Equal sums are necessary but not sufficient.

More Pseudocode and Code Tracing questions

Exams that ask this topic