Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the method printMessage inside main.
Java
public class Test { public static void main(String[] args) { [1](); } public static void printMessage() { System.out.println("Hello from printMessage"); } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
2fill in blank
mediumComplete the code to make methodB call methodC.
Java
public class StackExample { public static void methodA() { System.out.println("In methodA"); } public static void methodB() { System.out.println("In methodB"); [1](); } public static void methodC() { System.out.println("In methodC"); } public static void main(String[] args) { methodA(); methodB(); } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
3fill in blank
hardFix the error in the recursive method countdown by completing the base case condition.
Java
public class Recursion { public static void countdown(int n) { if (n [1] 0) { System.out.println("Done!"); } else { System.out.println(n); countdown(n - 1); } } public static void main(String[] args) { countdown(3); } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
4fill in blank
hardFill both blanks to complete the base case and recursive step of the fibonacci method.
Java
public class Fib { public static int fib(int n) { if (n [1] 1) { return n; } else { return fib(n - 1) + fib(n - [2]); } } public static void main(String[] args) { System.out.println(fib(4)); } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
5fill in blank
hardFill all three blanks to complete the mutually recursive even/odd checker methods.
Java
public class EvenOdd { public static boolean isEven(int n) { if (n == 0) { return true; } else { return [1](n - 1); } } public static boolean isOdd(int n) { if (n [2] 0) { return false; } else { return [3](n - 1); } } public static void main(String[] args) { System.out.println(isEven(4)); System.out.println(isOdd(3)); } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
