ProgrammingModerateAccenture · Cognizant GenC Next
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
String w[3] = {"flower", "flow", "flight"}
String p = w[0]
for (each i from 1 to 2)
while (w[i] does not start with p)
p = p without its last character
end while
end for
Print p, length(p)- A. flow 4
- B. f 1
- C. fli 3
- D. fl 2
Correct answer: D. fl 2
The candidate prefix shrinks until each word starts with it: flower → flow (fits flow), then flow → flo → fl (fits flight). The longest common prefix is fl: fl 2.
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? (substring(s, i, 2) is the 2 characters from index i; sorted(t) puts t's letters in…
- What will be the output of the following pseudocode? (strings are indexed from 0) String p = "ABABCABAB" Integer f[9], k = 0 f[0] = 0 for…
- What will be the output of the following pseudocode? Integer a[4] = {1, 2, 3, 4}, res[4], left = 1, right = 1 for (each i from 0 to 3)…
- What will be the output of the following pseudocode? Integer a[5] = {2, 3, -2, 4, -1} Integer mx = a[0], mn = a[0], best = a[0], t for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {-7, 1, 5, 2, -4, 3, 0} Integer total = 0…
- What will be the output of the following pseudocode? Integer a[5] = {1, 5, 3, 4, 2}, k = 2, c = 0 for (each i from 0 to 4) for (each j…