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

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

More Pseudocode and Code Tracing questions

Exams that ask this topic