Module: | Classes, Objects, OOPs & Inheritance
Q36: Consider the following statements regarding array bounds and the length property:
1. To determine the maximum number of elements an array can hold, Java provides a dedicated 'length()' execution method that must be invoked explicitly on the array object.
2. Attempting to access an index equal to or greater than the array's declared capacity will instantly trigger an 'ArrayIndexOutOfBoundsException' at runtime.
3. The physical capacity of a Java array is permanently locked once instantiated, meaning an array created with zero elements is legally valid but permanently immutable in size.
Which of the above statements is/are correct?
2. Attempting to access an index equal to or greater than the array's declared capacity will instantly trigger an 'ArrayIndexOutOfBoundsException' at runtime.
3. The physical capacity of a Java array is permanently locked once instantiated, meaning an array created with zero elements is legally valid but permanently immutable in size.
Which of the above statements is/are correct?
✅ Correct Answer: B
🎯 Quick Answer:
The correct combination is 2 and 3. Statement 1 is incorrect because 'length' is an implicitly defined final property (variable) of the array object, not a method; therefore, it is accessed without parentheses (e.g., array.length, not array.length()).Historical/Related Context: Buffer overflow attacks were the primary security vulnerability in legacy C applications because C did not check array boundaries; an attacker could overwrite adjacent memory blocks by simply writing past the end of an array.
Java completely eliminated this massive security flaw by enforcing strict, inescapable boundary checks on every single array memory access.
Causal Reasoning: An array of zero length (Statement 3) is frequently used in modern enterprise APIs to safely represent "no data available" instead of returning a hazardous null reference.
Because arrays are rigid, returning a zero-length array prevents NullPointerExceptions when the calling code attempts to iterate over the empty result in an enhanced for-loop.