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(89)
  1. A. 0
  2. B. true
  3. C. error
  4. D. false

Correct answer: D. false

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(89) = false because 89 is odd.

More Pseudocode and Code Tracing questions

Exams that ask this topic