ProgrammingModerateCognizant GenC Next · Accenture

What will be the output of the following pseudocode? (stop ends the program; code(c) gives a letter's character code)

What will be the output of the following pseudocode? (stop ends the program; code(c) gives a letter's character code)
String s = "PROGRAMMING"
Integer cnt[26] = all 0
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 i from 0 to length(s) - 1)
    if (cnt[code(s[i]) - code('A')] == 1)
        Print s[i]
        stop
    end if
end for
  1. A. P
  2. B. O
  3. C. A
  4. D. R

Correct answer: A. P

The first loop counts every letter; the second scans the string in order and stops at the first letter with count 1. P occurs once, so the output is P. O is the first non-repeating letter only if you start after P by mistake.

More Pseudocode and Code Tracing questions

Exams that ask this topic