ProgrammingVery hardGoldman Sachs OA · Infosys SP
What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; / is integer division; + joins strings)
What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; / is integer division; + joins strings)
Function f(String s)
Integer n = length(s)
if (n <= 2)
return s
end if
return f(substring(s, n / 2, n - n / 2)) + f(substring(s, 0, n / 2))
End Function
Print f("ABCDEFG")- A. GFEDCBA
- B. DEFGABC
- C. FGDEBCA
- D. FGBCDEA
Correct answer: C. FGDEBCA
Each call puts the second half before the first half, but pieces of length ≤ 2 are left as they are. f("DEFG") = f("FG") + f("DE") = FGDE and f("ABC") = f("BC") + f("A") = BCA, so the output is FGDEBCA. A full reversal (GFEDCBA) would need the recursion to go down to single letters.
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 0 end if if (n == 1) return 1 end if return…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {2, 0, 1, 2, 1, 0, 2, 1} Integer lo = 0…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {7, 2, 9, 4, 1, 8, 5} Integer pivot =…
- What will be the output of the following pseudocode? Integer a[9] = {3, 3, 4, 2, 4, 4, 2, 4, 2} Integer cand = 0, cnt = 0 for (each i from…
- What will be the output of the following pseudocode? Integer a[7] = {16, 17, 4, 3, 5, 2, 1} Integer mx = a[6], c = 1 for (each i from 5…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer nxt[6] = {3, 5, 0, 1, 2, 4} Integer x = 0, steps…