ProgrammingHardCognizant GenC Next · Accenture

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

What will be the output of the following pseudocode? (code(c) gives a letter's character code)
String s = "PROGRAMMING", r = ""
Boolean seen[26] = all false
for (each i from 0 to length(s) - 1)
    if (not seen[code(s[i]) - code('A')])
        r = r + s[i]
        seen[code(s[i]) - code('A')] = true
    end if
end for
Print r, length(r)
  1. A. PROGAMIN 7
  2. B. POAIN 5
  3. C. PROGAMIN 8
  4. D. PRGAMIN 7

Correct answer: C. PROGAMIN 8

Each letter is kept only the first time it appears: P, R, O, G, then R skipped, A, M, M skipped, I, N, G skipped. r = PROGAMIN with 8 letters.

More Pseudocode and Code Tracing questions

Exams that ask this topic