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
When do you use .length() and when do you use .length?
.length() is a method that you call to find the length of a String.
.length is a public instance variable that you have access to with the length of an Array.
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 shorthand or slang way of saying an Enhanced For Loop?
A for-each loop
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 value of this array at index 3?
String[] arr = new String[10]
The value is null, since the default value of a String is null.
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};
int[] grades = {100, 90, 75, 95}
If we wanted to use an enhanced for loop to iterate through this array, what would the Enhanced For Loop Variable be?
grade
for ( grade: grades )
What will this code print?
int[ ] scores = { 80, 92, 91, 68, 88}
for (int score : scores) {
System.out. print(score);
}
8092916888
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 are the 2 downsides of using an Enhanced For Loop?
An Enhanced For Loop cannot change any values in an array (works with a copy of the values), and it cannot do anything involving an index.
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.