Array Basics
Indexing
Common Errors
Traversals
Code Output
100

What is a 1D array?

A linear data structure that stores elements in a single row, accessible by index.

100

What is the index of the first element in a Java array?

0

100

What causes a NullPointerException when working with arrays?

Accessing or modifying an array that was never initialized

100

How do you loop through all elements in an array?

using a for loop:

for(int i = 0; i < arr.length; i++) { ... }


100

int[] a = {1, 2, 3};
System.out.println(a[1]);

2


200

What is the syntax to declare an array of 10 integers in Java?

int[] arr = new int[10];

200

What value does arr[arr.length - 1] access?

The last element of the array

200

What is wrong with this code? 

int[] x;  

x[0] = 5;

The array x is declared but not initialized

200

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

200

int[] b = {0, 1, 2, 3};
b[2] = 10;
System.out.println(b[2]);

10

300

How do you initialize an array with values {3, 5, 7}?

int[] arr = {3, 5, 7};

300

How do you return the length of an array? 

arr.length;

300

What happens when you try to access arr[10] in an array of size 5?

ArrayIndexOutOfBoundsException

300

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

300

int[] a = new int[4];
System.out.println(a[0]);

0

400

What is the length of the array int[] nums = new int[5];?

5

400

What is the output?

int[] a = {10, 20, 30};
System.out.println(a[2]);


30

400

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

400

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);

}

400

int[] arr = {2, 4, 6};
int sum = 0;
for(int x : arr) sum += x;
System.out.println(sum);

12

500

True or False — Array sizes can be changed after declaration.

False

500

What error occurs when accessing an index out of bounds?

ArrayIndexOutOfBoundsException

500

What’s wrong here? 

int[] arr = null;  

System.out.println(arr.length);


NullPointerException – array is not initialized

500

How do you find the maximum value in a 1D array?

int max = arr[0];  

for(int n : arr)  

  if(n > max) max = n;


500

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

M
e
n
u