The call stack is a special area in memory that stores information about active methods or functions. It keeps track of method calls and returns in the order they happen.
Click to reveal answer
beginner
What happens when a method is called in Java?
When a method is called, a new frame is added (pushed) to the top of the call stack. This frame holds the method's parameters, local variables, and return address.
Click to reveal answer
beginner
What does it mean when a method returns in terms of the call stack?
When a method finishes, its frame is removed (popped) from the call stack, and control goes back to the method below it on the stack.
Click to reveal answer
intermediate
What is a stack overflow error in Java?
A stack overflow error happens when the call stack runs out of space, usually because methods keep calling each other without stopping, like in infinite recursion.
Click to reveal answer
intermediate
How does the call stack help with debugging in Java?
The call stack shows the sequence of method calls that led to an error. This helps programmers understand where the problem happened and how the program reached that point.
Click to reveal answer
What is added to the call stack when a method is called?
AA copy of the entire program
BA new stack frame containing method details
CThe program's output
DA new thread
✗ Incorrect
When a method is called, a new stack frame with parameters, local variables, and return address is pushed onto the call stack.
What happens to the call stack when a method finishes execution?
AThe method's stack frame is popped off
BThe entire stack is cleared
CA new frame is added
DNothing changes
✗ Incorrect
When a method returns, its stack frame is removed (popped) from the call stack.
Which error is caused by the call stack running out of space?
ANullPointerException
BArrayIndexOutOfBoundsException
CClassNotFoundException
DStackOverflowError
✗ Incorrect
StackOverflowError occurs when the call stack exceeds its limit, often due to infinite recursion.
How does the call stack help during debugging?
AIt speeds up the program
BIt fixes the error automatically
CIt shows the sequence of method calls leading to an error
DIt hides errors from the user
✗ Incorrect
The call stack trace helps programmers see the order of method calls that caused an error.
Which of the following is NOT stored in a stack frame?
AGlobal variables
BMethod parameters
CReturn address
DLocal variables
✗ Incorrect
Global variables are not stored in the call stack; they are stored in a different memory area.
Explain what happens in the call stack when a method calls another method.
Think about how the program remembers where to return after the new method finishes.
You got /4 concepts.
Describe what causes a stack overflow error and how it relates to the call stack.
Imagine stacking too many plates without removing any.
You got /4 concepts.
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
Step 1: Understand call stack basics
The call stack keeps track of active method calls by adding a frame for each call.
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.
Final Answer:
It adds a new frame for the method call on top of the stack. -> Option A
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
Step 1: Recall Java method call syntax
In Java, methods are called by writing the method name followed by parentheses, e.g., processData();.
Step 2: Check each option
Only processData(); uses the correct syntax without extra keywords.
Final Answer:
processData(); -> Option C
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
Step 1: Trace method calls and prints
Main calls first(), which prints "A", then calls second() that prints "C".
Step 2: Return to first() and print remaining output
After second() finishes, first() prints "B".
Final Answer:
ACB -> Option D
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
Step 1: Analyze method calls
methodA() calls methodB(), which calls methodA() again, creating a loop.
Step 2: Understand call stack effect
This causes infinite recursion, adding frames endlessly until stack overflow occurs.
Final Answer:
Infinite recursion causing stack overflow error. -> Option A
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
Step 1: Understand factorial recursion
factorial(3) calls factorial(2), factorial(1), factorial(0), then returns 1.