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- A. 2
- B. 4
- C. 5
- 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.
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? Function F(Integer n) if (n == 0) return 1 end if return n - M(F(n - 1)) End Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {1, 2, 3, -2, 2, 1}, c = 0, s for (each i…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body) Integer c =…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "110100" Integer v = 0, p = 1 for (each i…
- What will be the output of the following pseudocode? Boolean mark[2..30] = all false Integer ops = 0, j for (each p from 2 to 30) if (p p…