Bird
Raised Fist0
Javaprogramming~20 mins

Call stack behavior in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Call Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this recursive call stack?

Consider the following Java code that uses recursion. What will be printed when countdown(3) is called?

Java
public class Test {
    public static void countdown(int n) {
        if (n == 0) {
            System.out.println("Done");
            return;
        }
        System.out.println(n);
        countdown(n - 1);
    }

    public static void main(String[] args) {
        countdown(3);
    }
}
A
Done
1
2
3
B
3
2
1
Done
C
3
2
1
DDone
Attempts:
2 left
💡 Hint

Think about how the function prints before the recursive call and what happens when n reaches zero.

❓ Predict Output
intermediate
2:00remaining
What is the value of variable after nested method calls?

Given the following Java code, what is the value of result after main finishes?

Java
public class Test {
    public static int addOne(int x) {
        return x + 1;
    }

    public static int doubleIt(int x) {
        return x * 2;
    }

    public static void main(String[] args) {
        int result = doubleIt(addOne(3));
        System.out.println(result);
    }
}
A8
B7
C6
D5
Attempts:
2 left
💡 Hint

Remember the order of method calls and how the call stack works.

❓ Predict Output
advanced
2:30remaining
What is printed by this mutual recursion example?

Examine the following Java code with two mutually recursive methods. What is the output when foo(2) is called?

Java
public class Test {
    public static void foo(int n) {
        System.out.println("foo " + n);
        if (n == 0) {
            System.out.println("foo done");
            return;
        }
        bar(n - 1);
    }

    public static void bar(int n) {
        if (n == 0) {
            System.out.println("bar done");
            return;
        }
        System.out.println("bar " + n);
        foo(n / 2);
    }

    public static void main(String[] args) {
        foo(2);
    }
}
A
foo 2
bar 1
foo 0
bar done
foo done
B
foo 2
bar 1
foo 1
bar 0
bar done
foo done
C
foo 2
bar 1
foo 0
foo done
D
foo 2
bar 1
foo 1
bar 0
foo done
bar done
Attempts:
2 left
💡 Hint

Trace the calls carefully, noting how n / 2 behaves with integer division.

🔧 Debug
advanced
1:30remaining
What error does this recursive method cause?

Consider this Java method that calls itself without a base case. What error will occur when infiniteRecursion() is called?

Java
public class Test {
    public static void infiniteRecursion() {
        infiniteRecursion();
    }

    public static void main(String[] args) {
        infiniteRecursion();
    }
}
ANo error, program runs forever
BNullPointerException
CIllegalArgumentException
DStackOverflowError
Attempts:
2 left
💡 Hint

Think about what happens when a method keeps calling itself without stopping.

🧠 Conceptual
expert
2:00remaining
How many stack frames are created during this recursive factorial call?

Given the following factorial method, how many stack frames are created when factorial(5) is called?

Java
public class Test {
    public static int factorial(int n) {
        if (n <= 1) return 1;
        return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        int result = factorial(5);
        System.out.println(result);
    }
}
A6
B5
C4
D1
Attempts:
2 left
💡 Hint

Count the initial call plus each recursive call until the base case.

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