ProgrammingModerateCognizant GenC Next · Accenture

What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; indices start at 0)

What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; indices start at 0)
String s = "ABABABA", t = "ABA"
Integer c = 0
for (each i from 0 to length(s) - length(t))
    if (substring(s, i, length(t)) == t)
        c = c + 1
    end if
end for
Print c
  1. A. 2
  2. B. 4
  3. C. 5
  4. D. 3

Correct answer: D. 3

The loop checks every start position 0 to 4, so overlapping matches count: ABA starts at 0, 2 and 4, giving 3. Counting without overlaps gives 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic