ProgrammingVery hardTCS NQT · Accenture
Pattern "aba" is searched in the text "abababa" with overlapping matches allowed. The procedure below counts the starting positions where the pattern occurs. What is printed?
Pattern "aba" is searched in the text "abababa" with overlapping matches allowed. The procedure below counts the starting positions where the pattern occurs. What is printed?
count = 0
for i = 0 to len(text) - len(pattern)
if text[i .. i+len(pattern)-1] == pattern then count = count + 1
end for
Print count- A. 2
- B. 3
- C. 4
- D. 7
Correct answer: B. 3
The matches start at positions 0, 2 and 4, which gives 3 overlapping occurrences. Skipping past each match gives only 2, counting the letter a gives 4, and printing the text length gives 7.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- For the string s = "forgeeksskeegfor", the procedure below expands around each centre to find the length of the longest palindromic…
- Count the palindromic substrings (substrings of length 1 or more that read the same forwards and backwards, counted by position) of the…
- Run-length encoding replaces each run of equal characters by the character followed by its run length. The procedure below encodes s =…
- A Caesar cipher shifts each lowercase letter forward by 3 places, wrapping from z back to a. The procedure below encodes the word "xyz"…
- The number of distinct arrangements (permutations of the letters, with identical letters not counted twice) of the string "AABBC" is…
- The string "Programming Language" is taken in lowercase and the procedure below counts its vowels (a, e, i, o, u). What is printed? count…