Complete the code to make the recursive method call itself with a smaller number.
public int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial([1]);
}
}The recursive call must reduce the problem size by calling factorial with n - 1.
Complete the code to correctly check the base case for the recursive Fibonacci method.
public int fibonacci(int n) {
if (n <= [1]) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}The base cases for Fibonacci are when n <= 1, returning n (0 or 1).
Fix the error in the recursive sum method by completing the base case condition.
public int sum(int[] arr, int index) { if (index [1] arr.length) { return 0; } else { return arr[index] + sum(arr, index + 1); } }
The base case is when index is greater than or equal to array length, stopping recursion.
Fill both blanks to complete the recursive method that prints numbers from n down to 1.
public void printNumbers(int n) {
if (n [1] 0) {
return;
}
System.out.println(n);
printNumbers(n [2] 1);
}The method stops when n is not greater than 0, and calls itself with n - 1 to count down.
Fill all three blanks to complete the recursive method that reverses a string.
public String reverse(String str) {
if (str.length() == 0) {
return "";
}
return str.charAt([1]) + reverse(str.[2]([3], str.length() - 1));
}The method takes the last character (index length-1) and adds it to the reverse of the substring from 0 to length-1.
