Bird
Raised Fist0
Javaprogramming~10 mins

Call stack behavior in Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 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
💡 Hint
Common Mistakes
Writing the method name without parentheses.
Trying to call main inside itself.
Using class name instead of method name.
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
💡 Hint
Common Mistakes
Calling methodA instead of methodC.
Trying to call main inside methodB.
Writing method name without parentheses.
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
💡 Hint
Common Mistakes
Using <= or < which may cause incorrect stopping.
Using > which never becomes true for countdown.
Not using equality operator for base case.
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
💡 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.
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
💡 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.

Practice

(1/5)
1. What does the call stack do in Java when a method is called?
easy
A. It adds a new frame for the method call on top of the stack.
B. It removes all previous method calls from memory.
C. It immediately executes all methods in the program.
D. It stores variables permanently for the entire program.

Solution

  1. Step 1: Understand call stack basics

    The call stack keeps track of active method calls by adding a frame for each call.
  2. Step 2: What happens on method call

    When a method is called, a new frame is pushed on top of the stack to hold its execution context.
  3. Final Answer:

    It adds a new frame for the method call on top of the stack. -> Option A
  4. Quick Check:

    Call stack adds frames on method calls [OK]
Hint: Call stack adds frames on method calls, removes on return [OK]
Common Mistakes:
  • Thinking call stack removes all frames on each call
  • Believing variables are stored permanently in call stack
  • Confusing call stack with heap memory
2. Which of the following is the correct way to call a method named processData in Java?
easy
A. run processData();
B. call processData();
C. processData();
D. execute processData();

Solution

  1. Step 1: Recall Java method call syntax

    In Java, methods are called by writing the method name followed by parentheses, e.g., processData();.
  2. Step 2: Check each option

    Only processData(); uses the correct syntax without extra keywords.
  3. Final Answer:

    processData(); -> Option C
  4. Quick Check:

    Method call syntax = methodName() [OK]
Hint: Method calls use methodName() syntax without extra words [OK]
Common Mistakes:
  • Adding keywords like 'call' or 'run' before method name
  • Missing parentheses after method name
  • Using incorrect keywords not in Java syntax
3. What will be the output of the following Java code?
public class Test {
    public static void main(String[] args) {
        first();
    }
    static void first() {
        System.out.print("A");
        second();
        System.out.print("B");
    }
    static void second() {
        System.out.print("C");
    }
}
medium
A. CAB
B. ABC
C. BAC
D. ACB

Solution

  1. Step 1: Trace method calls and prints

    Main calls first(), which prints "A", then calls second() that prints "C".
  2. Step 2: Return to first() and print remaining output

    After second() finishes, first() prints "B".
  3. Final Answer:

    ACB -> Option D
  4. Quick Check:

    Print order = A, C, B [OK]
Hint: Follow call order and prints step-by-step [OK]
Common Mistakes:
  • Ignoring that second() prints before first() resumes
  • Assuming prints happen after all calls finish
  • Mixing print order of method calls
4. Identify the error in this Java code related to call stack behavior:
public class Demo {
    public static void main(String[] args) {
        methodA();
    }
    static void methodA() {
        methodB();
    }
    static void methodB() {
        methodA();
    }
}
medium
A. Infinite recursion causing stack overflow error.
B. Syntax error due to missing return statements.
C. methodB() is never called.
D. No error; code runs normally.

Solution

  1. Step 1: Analyze method calls

    methodA() calls methodB(), which calls methodA() again, creating a loop.
  2. Step 2: Understand call stack effect

    This causes infinite recursion, adding frames endlessly until stack overflow occurs.
  3. Final Answer:

    Infinite recursion causing stack overflow error. -> Option A
  4. Quick Check:

    Recursive calls without base case cause stack overflow [OK]
Hint: Look for recursive calls without stopping condition [OK]
Common Mistakes:
  • Thinking missing return causes syntax error
  • Assuming code runs without error
  • Missing the infinite recursion loop
5. Given this Java code, what is the final output?
public class StackTest {
    public static void main(String[] args) {
        System.out.print(factorial(3));
    }
    static int factorial(int n) {
        if (n == 0) return 1;
        return n * factorial(n - 1);
    }
}

Consider how the call stack behaves during recursion.
hard
A. 9
B. 6
C. 3
D. 1

Solution

  1. Step 1: Understand factorial recursion

    factorial(3) calls factorial(2), factorial(1), factorial(0), then returns 1.
  2. Step 2: Calculate return values on stack unwind

    factorial(1) = 1 * 1 = 1, factorial(2) = 2 * 1 = 2, factorial(3) = 3 * 2 = 6.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    3! = 6 by recursive multiplication [OK]
Hint: Multiply down from base case as stack unwinds [OK]
Common Mistakes:
  • Confusing base case return value
  • Multiplying before recursive call returns
  • Misunderstanding recursion order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard