ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function isEven(Integer n)
    if (n == 0)
        return true
    end if
    return isOdd(n - 1)
end Function

Function isOdd(Integer n)
    if (n == 0)
        return false
    end if
    return isEven(n - 1)
end Function

Print isEven(22)
  1. A. true
  2. B. 0
  3. C. false
  4. D. error

Correct answer: A. true

isEven and isOdd call each other, each decrementing n by 1 and flipping the base case. The recursion bottoms out at n = 0, and isEven(22) = true because 22 is even.

More Pseudocode and Code Tracing questions

Exams that ask this topic