Heap memory is a part of memory where Java stores objects created during program execution. It is shared among all threads and managed by the Java Virtual Machine (JVM).
Click to reveal answer
beginner
Where are Java objects stored during runtime?
Java objects are stored in the heap memory, which allows dynamic allocation and garbage collection.
Click to reveal answer
intermediate
What role does the garbage collector play in heap memory?
The garbage collector automatically frees up heap memory by removing objects that are no longer referenced, helping to prevent memory leaks.
Click to reveal answer
intermediate
How does heap memory differ from stack memory in Java?
Heap memory stores objects and is shared across threads, while stack memory stores method calls and local variables and is thread-specific.
Click to reveal answer
advanced
What happens if the heap memory is full in a Java program?
If heap memory is full and no more objects can be allocated, the JVM throws an OutOfMemoryError, which usually crashes the program unless handled.
Click to reveal answer
Where are Java objects stored during program execution?
AHeap memory
BStack memory
CCPU registers
DMethod area
✗ Incorrect
Java objects are stored in heap memory, which is used for dynamic memory allocation.
What does the garbage collector do in heap memory?
AAllocates new objects
BCompiles Java code
CManages stack frames
DRemoves unreferenced objects
✗ Incorrect
The garbage collector frees heap memory by removing objects that are no longer referenced.
Which memory area is shared among all threads in Java?
AProgram counter
BStack memory
CHeap memory
DNative method stack
✗ Incorrect
Heap memory is shared among all threads, while stack memory is thread-specific.
What error occurs if heap memory is exhausted?
AOutOfMemoryError
BNullPointerException
CStackOverflowError
DClassNotFoundException
✗ Incorrect
OutOfMemoryError is thrown when the JVM cannot allocate more objects due to full heap memory.
Which of the following is NOT stored in heap memory?
AJava objects
BLocal variables
CInstance variables
DArrays
✗ Incorrect
Local variables are stored in stack memory, not heap memory.
Explain what heap memory is and why it is important in Java.
Think about where Java keeps objects created during the program.
You got /5 concepts.
Describe the difference between heap memory and stack memory in Java.
Compare what each memory area holds and how they behave.
You got /5 concepts.
Practice
(1/5)
1. Which of the following best describes heap memory in Java?
easy
A. It stores method call information and local variables.
B. It stores primitive variables like int and double.
C. It stores all objects created using new keyword.
D. It stores the Java bytecode instructions.
Solution
Step 1: Understand what heap memory stores
Heap memory is used to store objects created dynamically using the new keyword in Java.
Step 2: Differentiate from other memory areas
Primitive variables and method calls are stored on the stack, not heap. Bytecode is stored in method area, not heap.
Final Answer:
It stores all objects created using new keyword. -> Option C
Quick Check:
Heap memory = objects from new [OK]
Hint: Heap = objects created with new keyword [OK]
Common Mistakes:
Confusing stack with heap memory
Thinking primitives are stored in heap
Mixing method area with heap
2. Which of the following Java statements correctly creates an object stored in heap memory?
easy
A. String s = new String("hello");
B. int x = 5;
C. double y = 3.14;
D. boolean flag = true;
Solution
Step 1: Identify object creation syntax
Objects in Java are created using the new keyword, which allocates memory on the heap.
Step 2: Check each option
String s = new String("hello"); uses new String("hello"), which creates a String object on the heap. Other options declare primitives stored on the stack.
Final Answer:
String s = new String("hello"); -> Option A
Quick Check:
Object creation uses new keyword [OK]
Hint: Look for 'new' keyword to spot heap allocation [OK]
Common Mistakes:
Assuming primitives use heap
Confusing string literals with new String()
Ignoring that only new creates heap objects
3. What will be the output of the following Java code?
class Test {
public static void main(String[] args) {
String a = new String("abc");
String b = a;
a = null;
System.out.println(b);
}
}
medium
A. null
B. Runtime exception
C. Compilation error
D. abc
Solution
Step 1: Understand object references and heap
Variable a points to a String object "abc" in heap. Then b is assigned the same reference.
Step 2: Analyze effect of setting a to null
Setting a = null removes one reference, but b still points to the object, so it remains accessible.
Step 3: Print b
Printing b outputs the string "abc".
Final Answer:
abc -> Option D
Quick Check:
Reference b still points to "abc" [OK]
Hint: Nulling one reference doesn't delete object if others exist [OK]
Common Mistakes:
Assuming object is deleted when one reference is null
Confusing references with objects
Expecting null output incorrectly
4. Identify the error in the following Java code related to heap memory usage:
public class Demo {
public static void main(String[] args) {
int[] arr = new int[5];
arr = null;
System.out.println(arr.length);
}
}
medium
A. No error, prints 5
B. NullPointerException at runtime
C. Compilation error due to null assignment
D. Array size becomes zero
Solution
Step 1: Understand array creation and null assignment
The array is created on heap with size 5, then arr reference is set to null.
Step 2: Analyze printing arr.length
Since arr is null, accessing arr.length causes a NullPointerException at runtime.
Final Answer:
NullPointerException at runtime -> Option B
Quick Check:
Accessing member on null reference throws exception [OK]
class Node {
int value;
Node next;
Node(int val) { value = val; }
}
public class Test {
public static void main(String[] args) {
Node n1 = new Node(1);
Node n2 = new Node(2);
n1.next = n2;
n2.next = n1;
n1 = null;
n2 = null;
// What happens to the Node objects in heap?
}
}
What happens to the two Node objects in heap after n1 and n2 are set to null?
hard
A. Both objects become unreachable and eligible for garbage collection.
B. Objects remain in heap because they reference each other, preventing garbage collection.
C. Only n1 object is collected, n2 remains.
D. Objects cause a memory leak and crash the program.
Solution
Step 1: Analyze object references after null assignments
Both n1 and n2 references are set to null, so no external references point to these objects.
Step 2: Understand garbage collection with cyclic references
Even though the two objects reference each other, Java's garbage collector detects unreachable cycles and frees them.
Final Answer:
Both objects become unreachable and eligible for garbage collection. -> Option A
Quick Check:
Unreachable objects, even cyclic, are collected [OK]
Hint: Java GC collects unreachable cyclic objects too [OK]