ProgrammingHardAccenture · Capgemini
What will be the output of the following pseudocode? (Strings are indexed from 0)
What will be the output of the following pseudocode? (Strings are indexed from 0)
Integer calls = 0
Function pal(String s, Integer i, Integer j)
calls = calls + 1
if (i >= j)
return true
end if
if (s[i] != s[j])
return false
end if
return pal(s, i + 1, j - 1)
End Function
Boolean r = pal("RACECARS", 0, 7)
Boolean t = pal("REFER", 0, 4)
Print r, t, calls- A. false true 4
- B. true true 4
- C. false true 6
- D. false true 3
Correct answer: A. false true 4
RACECARS fails at once (R ≠ S): 1 call, false. REFER makes the calls (0, 4), (1, 3) and (2, 2), where i = j ends it: 3 calls, true. Total calls 1 + 3 = 4.
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? Integer s = 0, i = 0 while (i < 10) i = i + 1 if (i mod 3 == 0) continue end if s = s…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {10, 20, 30, 40, 50}, b[5], k = 2, n = 5…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "ABBCCCDDDD" Integer best = 0, run = 1 for…
- What will be the output of the following pseudocode? Integer M[2][2] = {{1, 1}, {1, 0}}, R[2][2] = {{1, 0}, {0, 1}}, T[2][2] for (each k…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer s = 0 for (each i from…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 360, d = 2, s = 0…