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
  1. A. false true 4
  2. B. true true 4
  3. C. false true 6
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic