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)
  1. A. flow 4
  2. B. f 1
  3. C. fli 3
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic