ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
Scanning "abcab dab" as a string of letters, the procedure below counts the adjacent pairs that read "ab" (both letters next to each other, in order). The text is "abcabdab". What is printed?
Scanning "abcab dab" as a string of letters, the procedure below counts the adjacent pairs that read "ab" (both letters next to each other, in order). The text is "abcabdab". What is printed?
count = 0
for i = 0 to n - 2
if s[i] == 'a' and s[i+1] == 'b' then count = count + 1
end for
Print count- A. 0
- B. 3
- C. 8
- D. 2
Correct answer: B. 3
The ab pairs start at positions 0, 3 and 6, which gives 3. Counting the ba pairs gives 0, printing the length gives 8, and stopping the scan two early misses the final pair and gives 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Two words are checked as anagrams by comparing letter counts. The procedure below sums the absolute differences between the letter counts…
- The minimum number of insertions or deletions needed to make the string "abcca" a palindrome is len(s) - LPS(s), where LPS is the length…
- The edit distance (Levenshtein, insert/delete/replace each cost 1) between "kitten" and "sitting" is computed by the standard table. What…
- The string " hello world " is split by the Python-style rule that runs of spaces are treated as one separator and empty pieces are…
- The longest common subsequence (LCS) of "ABCBDAB" and "BDCABA" is computed by the standard table. The procedure below prints its length…
- The number of non-empty substrings of "abcd" (substrings are contiguous pieces, counted by position) is computed by the procedure below…