Write a recursive Java method to compute the nth term of the Fibonacci sequence, taking the first two terms as 0 and 1.
Recursion
Original Khojo Papers practice question — not from a past board paper.
Recursion gives shorter and more natural code for problems that are themselves defined recursively, such as tree traversal, and it is easier to reason about. Against that, each call uses stack space so deep recursion may overflow the stack, and the repeated calls make it slower than the equivalent loop.
No citable source has been recorded for this record. Treat it as practice material, not as fact.
Any recursive algorithm can be rewritten iteratively, sometimes at the cost of clarity.
From the same topic and chapter, at a similar level.
Write a recursive Java method to compute the nth term of the Fibonacci sequence, taking the first two terms as 0 and 1.
Recursion
Write a recursive Java method to find the greatest common divisor of two positive integers.
Recursion
How many times is the method called in total when fact(5) is executed, taking fact(0) as the base case?
Recursion
Write a recursive Java method to reverse a string.
Recursion
Write a recursive Java method to compute the factorial of a non-negative integer n.
Recursion
State and verify the absorption law X + X·Y = X using a truth table.
Boolean Algebra