Exams Knowledge Hub

MCQs for Competitive Exams, School & College Exams

Module: | Core Architecture, Basics & Control Flow

Q10: Consider the following statements regarding memory allocation for Primitive and Reference data types in Java:

1. Local primitive variables declared inside a method are consistently allocated a fixed amount of space directly in the Stack memory layer.
2. Reference variables store the actual complete object data directly within the Stack memory to optimize the application's runtime access speed.
3. If a primitive data type is declared as an instance variable within a class definition, it is stored in the Heap memory alongside its parent object instance.

Which of the above statements is/are correct?
A
Only 1 and 2
B
Only 2 and 3
C
Only 1 and 3
D
1, 2, and 3
✅ Correct Answer: C
🎯 Quick Answer:
The correct combination is 1 and 3. Statement 2 is incorrect because reference variables do not store actual object data in the Stack; they merely store the memory address (pointer) in the Stack, while the heavy object data itself is allocated dynamically in the Heap.
Concept Definition: Memory allocation in Java strictly divides variables into two storage contexts depending on their data type and scope: the Stack (for execution threads and local primitives) and the Heap (for dynamically created objects). Structural Breakdown: When a method is called, a new block (frame) is created on the Stack.
Local primitives (like int x = 5) go directly inside this frame.
When an object is instantiated (like new Car()), the Car object goes to the Heap, and a reference pointing to that Heap location is kept in the Stack frame.
Historical/Related Context: This dual-memory approach solves a major performance bottleneck.
Objects can be massive and vary in size, so throwing them onto the highly structured, fast-moving Stack would cause immediate memory overflow.
Causal Reasoning: Instance variables (even primitives) are stored in the Heap (Statement 3) because they define the state of an object.
Since the object physically resides in the Heap, all of its internal state variables must logically be packaged alongside it within that same Heap memory block.