Imagine you want to store the scores of 5 students. Why is using an array better than using 5 separate variables?
Think about how many variables you would need without arrays and how easy it is to loop through them with arrays.
Arrays group multiple values under a single name and allow easy access using indexes. This reduces code repetition and helps manage data efficiently.
What is the output of this Java code?
int[] numbers = {10, 20, 30, 40, 50}; System.out.println(numbers[2]);
Remember, array indexes start at 0 in Java.
The element at index 2 is the third element, which is 30.
What error does this Java code produce?
int[] arr = {1, 2, 3}; System.out.println(arr[3]);
Check if the index 3 is valid for an array of size 3.
Arrays in Java have indexes from 0 to length-1. Accessing index 3 in a 3-element array causes ArrayIndexOutOfBoundsException.
Given this Java array declaration, how many elements does it contain?
String[] fruits = new String[7];
The number inside the brackets when creating the array shows its size.
The array is created with size 7, so it can hold 7 elements indexed 0 to 6.
Which reason best explains why arrays improve efficiency in programs?
Think about how computers access memory and how arrays organize data.
Arrays store elements in continuous memory blocks, so accessing any element by index is very fast compared to scattered storage.
