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- A. S 4
- B. I 5
- C. I 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.
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? (All variables are integers, so / is integer division; arrays are indexed from 0)…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 10) for (each j from 1 to i) if (j j > i) break…
- What will be the output of the following pseudocode? Integer m[3][3] = {{5, 1, 7}, {2, 9, 3}, {8, 4, 6}} Integer c = 0 Boolean peak for…
- What will be the output of the following pseudocode? String s = "CODE IS FUN", r = "", w = "" for (each i from 0 to length(s) - 1) if…
- What will be the output of the following pseudocode? (substring(t, i, k) is the k characters starting at index i; indices start at 0)…
- What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift) Integer a = 13, b = 11, carry, rounds = 0…