'F' → Fullscreen
// Figure 1 — Predict the output
public static void mystery(int n){ if (n == 0) { return; }
System.out.println(n); mystery(n - 1);}// Figure 2 — Predict the return value
public static int mystery(int n){ if (n == 1) { return 1; }
return n * mystery(n - 1);}// Figure 3 — Recursive string length
public static int length(String s){ if (s.equals("")) { return 0; }
return 1 + length(s.substring(1));}// Figure 4 — Check if array is sorted
public static boolean isSorted(int[] arr, int index){ if (index >= arr.length - 1) { return true; }
if (arr[index] > arr[index + 1]) { return false; }
return isSorted(arr, index + 1);}// Task 1 — Trace the Output//// What does this program print?//// mystery(4);
public static void mystery(int n){ if (n == 0) { return; }
mystery(n - 1); System.out.println(n);}
// Write the output as comments.// Task 2 — Sum of Digits//// Write a recursive method://// digitSum(int n)//// Examples:// digitSum(538) → 16// digitSum(42) → 6//// Hint:// last digit = n % 10// remaining digits = n / 10
public class Program{ public static void main(String[] args) { System.out.println(digitSum(538)); }
public static int digitSum(int n) { // Write your code here return 0; }}// Task 3 — Reverse a String//// Write a recursive method://// reverse(String s)//// Example:// reverse("hello") → "olleh"
public class Program{ public static void main(String[] args) { System.out.println(reverse("hello")); }
public static String reverse(String s) { // Write your code here return ""; }}// Task 4 — Check if Array is Sorted//// Write a recursive method://// isSorted(int[] arr, int index)//// It should return true if the array// is sorted in ascending order.
public class Program{ public static void main(String[] args) { int[] a = {1, 3, 5, 7}; int[] b = {2, 4, 3, 8};
System.out.println(isSorted(a, 0)); System.out.println(isSorted(b, 0)); }
public static boolean isSorted(int[] arr, int index) { // Write your code here return false; }}// Task 5 — Final Challenge//// Write a recursive method://// countVowels(String s)//// It should count how many vowels// appear in the string.//// Example:// countVowels("recursion") → 4
public class Program{ public static void main(String[] args) { System.out.println(countVowels("recursion")); }
public static int countVowels(String s) { // Write your code here return 0; }}1234
16ollehtruefalse4Submit your completed activity.