0
0
Javaprogramming~15 mins

Call stack behavior in Java - Interactive Code Practice

Choose your learning style8 modes available
ads_clickPractice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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
Amain
BprintMessage
CTest
DSystem.out.println
Attempts:
3 left
2fill in blank
medium

Complete 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
AmethodA
Bmain
CmethodC
DSystem.out.println
Attempts:
3 left
3fill in blank
hard

Fix 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
A==
B>
C<=
D<
Attempts:
3 left
4fill in blank
hard

Fill 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
A<=
B2
C==
D1
Attempts:
3 left
5fill in blank
hard

Fill 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
AisOdd
BisEven
C==
D>
Attempts:
3 left