What is a 1D array?
A linear data structure that stores elements in a single row, accessible by index.
What is the index of the first element in a Java array?
0
What causes a NullPointerException when working with arrays?
Accessing or modifying an array that was never initialized
How do you loop through all elements in an array?
using a for loop:
for(int i = 0; i < arr.length; i++) { ... }
int[] a = {1, 2, 3};
System.out.println(a[1]);
2
What is the syntax to declare an array of 10 integers in Java?
int[] arr = new int[10];
What value does arr[arr.length - 1] access?
The last element of the array
What is wrong with this code?
int[] x;
x[0] = 5;
The array x is declared but not initialized
What's the output?
int[] a = {2, 4, 6};
for(int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
2 4 6
int[] b = {0, 1, 2, 3};
b[2] = 10;
System.out.println(b[2]);
10
How do you initialize an array with values {3, 5, 7}?
int[] arr = {3, 5, 7};
How do you return the length of an array?
arr.length;
What happens when you try to access arr[10] in an array of size 5?
ArrayIndexOutOfBoundsException
What does this code segment do?
int count = 0;
for(int n : arr) {
if(n % 2 == 0) count++;
}
counts how many elements in an array are even
int[] a = new int[4];
System.out.println(a[0]);
0
What is the length of the array int[] nums = new int[5];?
5
What is the output?
30
Identify the bug:
int[] nums = new int[3];
for(int i = 0; i <= nums.length; i++) {
nums[i] = i;
}
The loop should be i < nums.length to avoid accessing out of bounds
What is the correct syntax for using a for-each loop to print every element in the array nums?
for(int num : nums) {
System.out.println(num);
}
int[] arr = {2, 4, 6};
int sum = 0;
for(int x : arr) sum += x;
System.out.println(sum);
12
True or False — Array sizes can be changed after declaration.
False
What error occurs when accessing an index out of bounds?
ArrayIndexOutOfBoundsException
What’s wrong here?
int[] arr = null;
System.out.println(arr.length);
NullPointerException – array is not initialized
How do you find the maximum value in a 1D array?
int max = arr[0];
for(int n : arr)
if(n > max) max = n;
int[] nums = {3, 1, 4, 1, 5};
int total = 0;
for(int i = 0; i < nums.length; i++)
total += nums[i];
System.out.println(total);
14