What is the term for a collection of elements of the same type in Java?
Array
What is wrong with this code?
int[] arr = {1, 2, 3, 4}
Missing semicolon at the end.
Correct code: int[] arr = {1, 2, 3, 4};
What is the term for the fixed size of an array in Java?
Array length
What will this code output? int[] arr = {1, 2, 3}; System.out.println(arr[0]);
1
Can the size of a Java array be changed after it is created?
No, the size of an array in Java is fixed once initialized
What do you call the position of an element in an array?
Index
How do you assign a value of 10 to the second element of an array arr in Java?
arr[1] = 10;
What is an "index out of bounds" error in Java?
Accessing an array element with an invalid index
What will this code output? int[] arr = {4, 5, 6}; System.out.println(arr.length);
3
What is the return type of the Arrays.sort() method in Java?
void (It sorts the array in place)
What does it mean when an array is "empty" in Java?
he array has no elements or is initialized with no values.
How do you access the third element in an array arr in Java?
arr[2]
What is the term for an array with multiple dimensions, like a matrix?
Multidimensional array
What is wrong with this code? int[] arr = new int[5]; arr[5] = 10;
It causes an "IndexOutOfBoundsException" because the valid indices are 0 to 4.
What is the difference between a String[] and an int[] array in Java?
A String[] holds string values, and an int[] holds integer values.
What is the name of the method used to sort an array in Java?
sort()
What is wrong with this code?
int[] arr = new int[];
The array size or elements are missing. It should be int[] arr = new int[5]; or int[] arr = {1, 2, 3};
What does the clone() method do to an array in Java?
It creates a shallow copy of the array.
How do you fill an array with the value 5 in Java?
Arrays.fill(arr, 5);
How do you insert the value 50 at the second position of an array arr = {10, 20, 30, 40} in Java?
In Java, arrays have a fixed size, so you cannot directly insert into an existing array. You would need to create a new array and copy the elements.
What is an "index out of bounds" error in Java?
Accessing an array element with an invalid index
What will this code print?
int[] arr = {10, 20, 30}; System.out.println(arr[3]);
It will throw an ArrayIndexOutOfBoundsException because the index 3 is out of bounds.
What does it mean for an array to be "sparse" in Java?
The array contains many unused elements (often null)
What will this code print?
int[] arr = {10, 20, 30}; for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
10
20
30
Can you give a real-life example of how arrays are used in daily life?
A bookshelf. Each position on the shelf (index) holds a book (element), and the number of books is fixed (array size). If you have a shelf with 5 spaces, you can only store 5 books.