Exams Knowledge Hub

MCQs for Competitive Exams, School & College Exams

Module: | Classes, Objects, OOPs & Inheritance

Q34: Consider the following statements regarding array memory allocation in Java:

1. In Java, arrays are dynamically allocated strictly in the Heap memory, even if they are defined to exclusively store primitive data types like 'int' or 'char'.
2. When an array of object references is initialized without providing explicit elements, the Java compiler automatically populates every index slot with the 'null' reference to prevent garbage data reads.
3. The physical size of a Java array can be dynamically increased after initialization by utilizing the standard assignment operator to insert new values beyond its original declared capacity.

Which of the above statements is/are correct?
A
Only 1 and 2
B
Only 1 and 3
C
Only 2 and 3
D
1, 2, and 3 ' is executed, a single object representing the entire array is created on the Heap. The reference to this massive block is stored on the Stack. For primitives, the array slots hold actual zeroed values. For objects, the slots act as pointers holding null references until actual objects are explicitly assigned. Historical/Related Context: Java arrays were heavily modeled after C++ arrays but with added memory safety. In C, reading uninitialized array indices returned dangerous random memory leftovers (garbage data). Java structurally prevents this catastrophic behavior by aggressively zeroing out primitive arrays and nullifying object arrays immediately upon instantiation. Causal Reasoning: Fixed capacity ensures ultra-fast memory reads. Because the compiler guarantees the exact memory footprint size at instantiation, the JVM can instantly calculate the precise memory address of any index mathematically without traversing a complex node chain, ensuring absolute O(1) time complexity for data retrieval.]
✅ Correct Answer: A
🎯 Quick Answer:
The correct combination is 1 and 2. Statement 3 is incorrect because standard Java arrays have a strictly fixed length; their capacity cannot be mutated once instantiated in memory.
Concept Definition: An array is a statically-sized, linear data structure that stores a contiguous collection of elements sharing the exact same data type.
Structural Breakdown: When the syntax 'new int[5