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"); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Writing the method name without parentheses.
Trying to call
main inside itself.Using class name instead of method name.
✗ Incorrect
The method
printMessage is called inside main by writing its name followed by parentheses.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(); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Calling
methodA instead of methodC.Trying to call
main inside methodB.Writing method name without parentheses.
✗ Incorrect
Inside
methodB, calling methodC adds it to the call stack after methodB.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); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using
<= or < which may cause incorrect stopping.Using
> which never becomes true for countdown.Not using equality operator for base case.
✗ Incorrect
The base case should check if
n == 0 to stop the recursion and print "Done!".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)); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using == 1 instead of <= 1 for the base case.
Using n-1 instead of n-2 for the second recursive call.
Using n == 0 only for base case.
✗ Incorrect
The base case is
n <= 1 return n; recursive step fib(n-1) + fib(n-2). Each recursive call adds frames to the call stack, creating a tree of calls.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)); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Having both methods call the same method (direct recursion instead of mutual).
Using wrong comparison like
> for base case.Swapping which method calls which.
✗ Incorrect
isEven calls isOdd(n-1); isOdd base case n == 0 false else isEven(n-1). This alternates method frames on the call stack.