Stack memory is a special area of memory that stores method calls, local variables, and references during program execution. It works like a stack of plates, where the last item added is the first to be removed (LIFO).
Click to reveal answer
beginner
How does stack memory handle method calls?
Each time a method is called, a new block called a stack frame is created on top of the stack to hold that method's local variables and data. When the method finishes, its stack frame is removed.
Click to reveal answer
intermediate
What happens if the stack memory is full?
If the stack memory runs out of space, a StackOverflowError occurs. This usually happens when methods call themselves too many times (deep recursion).
Click to reveal answer
beginner
What kind of data is stored in stack memory?
Stack memory stores local variables, method parameters, and return addresses. It does NOT store objects themselves; objects are stored in heap memory, but references to them are kept on the stack.
Click to reveal answer
intermediate
Why is stack memory faster than heap memory?
Stack memory is faster because it uses a simple last-in, first-out structure and fixed size blocks for method calls. Heap memory is more complex because it manages dynamic memory allocation for objects.
Click to reveal answer
What does stack memory store in Java?
AClass definitions
BObjects and their data
CStatic variables
DLocal variables and method calls
✗ Incorrect
Stack memory stores local variables and method call information, not objects themselves.
What error occurs when stack memory is full?
AStackOverflowError
BOutOfMemoryError
CNullPointerException
DClassNotFoundException
✗ Incorrect
StackOverflowError happens when the stack runs out of space, often due to deep or infinite recursion.
Which memory area stores Java objects?
AStack memory
BMethod area
CHeap memory
DRegister
✗ Incorrect
Objects are stored in heap memory; stack memory holds references to these objects.
What is a stack frame?
AA block of memory for an object
BA block of memory for a method call
CA type of exception
DA Java class
✗ Incorrect
A stack frame holds data for one method call, including local variables and return address.
Why is stack memory faster than heap memory?
ABecause it uses a simple last-in, first-out structure
BBecause it stores fewer data types
CBecause it is stored on the hard drive
DBecause it uses garbage collection
✗ Incorrect
Stack memory is faster due to its simple LIFO structure and fixed size allocations.
Explain how stack memory works during method calls in Java.
Think about how a stack of plates works when you add or remove plates.
You got /4 concepts.
Describe the difference between stack memory and heap memory.
Consider where variables and objects live in memory.
You got /4 concepts.
Practice
(1/5)
1. What does stack memory primarily store in Java?
easy
A. Method calls and local variables
B. Objects and class variables
C. Static variables and constants
D. Files and network connections
Solution
Step 1: Understand stack memory role
Stack memory stores data related to method execution like local variables and method calls.
Step 2: Differentiate from heap memory
Heap stores objects, while stack stores method calls and local variables.
Final Answer:
Method calls and local variables -> Option A
Quick Check:
Stack memory = method calls + local variables [OK]
Hint: Stack = method calls + local variables [OK]
Common Mistakes:
Confusing stack with heap memory
Thinking objects are stored in stack
Mixing static variables with stack data
2. Which of the following is the correct way to declare a local variable inside a method in Java?
easy
A. static int x = 10;
B. local int x = 10;
C. var int x = 10;
D. int x = 10;
Solution
Step 1: Recall local variable syntax
Local variables inside methods are declared simply with type and name, e.g., int x = 10;
Step 2: Identify invalid keywords
Keywords like 'local' or 'var int' are invalid in Java, and 'static' is not allowed for local variables.
Final Answer:
int x = 10; -> Option D
Quick Check:
Local variable syntax = type + name + value [OK]
Hint: Local vars: type name = value; no extra keywords [OK]
Common Mistakes:
Using 'local' keyword which doesn't exist
Trying to use 'static' inside methods
Confusing 'var' usage in older Java versions
3. What will be the output of this Java code?
public class Test {
public static void main(String[] args) {
int a = 5;
int b = 10;
int c = add(a, b);
System.out.println(c);
}
public static int add(int x, int y) {
return x + y;
}
}
medium
A. 15
B. 510
C. Compilation error
D. Runtime error
Solution
Step 1: Trace method calls and parameters
Main calls add(5, 10), passing values 5 and 10 as local variables on the stack.
Step 2: Calculate return value
Add method returns 5 + 10 = 15, which is stored in c and printed.
Final Answer:
15 -> Option A
Quick Check:
5 + 10 = 15 [OK]
Hint: Add method returns sum; print result [OK]
Common Mistakes:
Concatenating numbers as strings
Confusing method parameters with global variables
Expecting runtime or compile errors
4. Identify the error in this Java code related to stack memory usage:
public class Demo {
public static void main(String[] args) {
int result = factorial(4);
System.out.println(result);
}
public static int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
}
medium
A. Stack overflow due to too many local variables
B. Missing base case causing infinite recursion
C. No error, code runs correctly
D. Incorrect method return type
Solution
Step 1: Check recursion base case
The factorial method has a base case: if n == 0 return 1, stopping recursion.
Step 2: Confirm recursion correctness
Each recursive call reduces n by 1, so recursion ends properly without stack overflow.
Final Answer:
No error, code runs correctly -> Option C
Quick Check:
Base case present, recursion safe [OK]
Hint: Check for base case to avoid stack overflow [OK]
Common Mistakes:
Assuming missing base case when it exists
Confusing local variables with stack overflow cause
Thinking return type is wrong
5. Consider this recursive method that calculates the sum of numbers from n down to 1:
public static int sum(int n) {
if (n <= 0) return 0;
else return n + sum(n - 1);
}
What will happen if you call sum(1000000) in Java, and why?
hard
A. It will return the correct sum without issues
B. It will cause a stack overflow error due to deep recursion
C. It will run infinitely without stopping
D. It will cause a compile-time error
Solution
Step 1: Analyze recursion depth
Calling sum(1000000) causes 1,000,000 recursive calls, each adding a stack frame.
Step 2: Understand stack memory limits
Java's stack size is limited; deep recursion like this exceeds it, causing stack overflow error.
Final Answer:
It will cause a stack overflow error due to deep recursion -> Option B
Quick Check:
Deep recursion > stack size = stack overflow [OK]
Hint: Very deep recursion causes stack overflow error [OK]
Common Mistakes:
Assuming recursion always works regardless of depth
Thinking it runs infinitely without base case
Confusing runtime errors with compile errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.