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- A. P
- B. O
- C. A
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {1, 1, 2, 2, 2, 3, 5, 5} Integer k = 1 for…
- What will be the output of the following pseudocode? (strings are indexed from 0) String x = "AGGTAB", y = "GXTXAYB" Integer L[0..6][0..7]…
- What will be the output of the following pseudocode? Integer wt[3] = {2, 3, 4}, val[3] = {3, 5, 6} Integer best[0..6] = all 0 for (each i…
- What will be the output of the following pseudocode? Integer lcm = 18, steps = 0 Integer a = 12, b = 18 while (lcm mod a != 0 OR lcm mod b…
- What will be the output of the following pseudocode? Integer total = 0 Procedure p(Integer n) if (n <= 0) return end if total = total + n…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 8) for (each j from i + 1 to 9) for (each k from…