ProgrammingHardCognizant GenC Next · Accenture

What will be the output of the following pseudocode? (code(c) gives a letter's character code and char(k) the reverse)

What will be the output of the following pseudocode? (code(c) gives a letter's character code and char(k) the reverse)
String s = "MISSISSIPPI"
Integer cnt[26] = all 0, best = 0
Char ans
for (each i from 0 to length(s) - 1)
    cnt[code(s[i]) - code('A')] = cnt[code(s[i]) - code('A')] + 1
end for
for (each k from 0 to 25)
    if (cnt[k] > best)
        best = cnt[k]
        ans = char(code('A') + k)
    end if
end for
Print ans, best
  1. A. S 4
  2. B. I 5
  3. C. I 4
  4. D. P 2

Correct answer: C. I 4

I and S both occur 4 times. The second loop scans the alphabet and replaces ans only on a strictly larger count, so the first of the tied letters, I, is kept: I 4. With >= it would print S 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic