ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (counting pattern occurrences, allowing overlaps)?
What will be the output of the following pseudocode (counting pattern occurrences, allowing overlaps)?
Function countOcc(String text, String pat)
Integer c = 0
Integer n = length(text), m = length(pat)
for (each i from 0 to n - m)
if (substring(text, i, i + m) == pat)
c = c + 1
end if
end for
return c
End Function
Print countOcc("abababab", "aba")- A. 2
- B. 4
- C. 3
- D. 1
Correct answer: C. 3
Checking every starting index, 'aba' matches at positions 0 ('abab...'->'aba'), 2 ('abab'->'aba'), and 4, giving 3 overlapping matches in 'abababab'.
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 (manually parsing a signed integer string)? Function strToInt(String s) Integer sign =…
- What will be the output of the following pseudocode (counting trailing zeros in n factorial, via counting factors of 5)? (All variables…
- What will be the output of the following pseudocode (fast exponentiation)? (All variables are integers, so / is integer division.) Integer…
- What will be the output of the following pseudocode (checking if one string is a rotation of another)? Function isRotation(String a…
- What will be the output of the following pseudocode (counting total set bits across 1..n)? Function countSetBitsRange(Integer n) Integer…
- What will be the output of the following pseudocode (depth-first traversal of a graph given as an adjacency list)? Map graph = {0: [1, 2]…