ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
The procedure below counts the positions i in "abbab" where the two-letter window s[i]s[i+1] is "ab" or "ba". What is printed?
The procedure below counts the positions i in "abbab" where the two-letter window s[i]s[i+1] is "ab" or "ba". What is printed?
count = 0
for i = 0 to n - 2
if s[i..i+1] is "ab" or "ba" then count = count + 1
end for
Print count- A. 2
- B. 1
- C. 3
- D. 4
Correct answer: C. 3
The two-letter windows of abbab are ab, bb, ba and ab, and the ab or ba ones are 3. Counting only ab gives 2, counting bb gives 1, and the number of adjacent pairs is 4.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below counts the positions i in "mississippi" where the two letters s[i]s[i+1] are both "s" (overlaps allowed, though "ss"…
- The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of…
- The number of distinct substrings (non-empty, counted once per distinct text) of the string "aab" is computed by the procedure below using…
- Two positions in the word "hello" are chosen (the letters at the two positions form a pair, and positions are distinct). The procedure…
- The procedure below counts the cyclic rotations of "abab" that are equal to "abab" itself, checking the rotations obtained by moving k…
- Coin denominations are 1, 4 and 6. The greedy method below pays amount 8 by always taking the largest coin that still fits (coins are…