Which of the following is False about arrays in Java?
A data structure for storing a collection of data of the same type.
Length of array can be changed after creation of array.
A Java array is an object.
Array’s index cannot be negative or bigger than array’s length.
The Correct Answer and Explanation is:
The correct answer is:
“Length of array can be changed after creation of array.” – This is FALSE.
Detailed Explanation:
In Java, an array is a data structure that holds a fixed number of elements of the same type. Once an array is created, its size cannot be changed, which makes the statement “Length of array can be changed after creation of array” false.
Let’s break down the other options:
- “A data structure for storing a collection of data of the same type.”
✅ True – Arrays in Java are homogeneous, meaning they store multiple elements of the same data type (e.g.,int[],String[]). This ensures type safety and efficient memory management. - “A Java array is an object.”
✅ True – Even if the array holds primitive types (likeintorchar), the array itself is still an object in Java. This means it has methods and attributes like.length, and it’s allocated on the heap. - “Array’s index cannot be negative or bigger than array’s length.”
✅ True – Array indexing in Java starts from0and goes up tolength - 1. Trying to access an index outside this range, such as a negative index or an index equal to or greater than the array’s length, results in aArrayIndexOutOfBoundsException.
Why the “length cannot be changed” is False:
Once you declare an array, for example:
int[] numbers = new int[5];
This array will always have 5 elements. You cannot resize it. If you need a resizable structure, you should use ArrayList instead of a traditional array.
So if you needed to “resize” an array, you’d have to create a new one and copy the old contents manually or using Arrays.copyOf().
Summary:
- Java arrays are fixed in size after creation.
- They are objects.
- They store elements of the same type.
- Accessing indices out of bounds throws an exception.
So, the false statement is:
👉 “Length of array can be changed after creation of array.”