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

Correct answer: B. 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(37) = false because 37 is odd.

More Pseudocode and Code Tracing questions

Exams that ask this topic