Array Basics in AP Computer Science A
What will be the output of the following code?
java
int[] arr = new int[3];
System.out.println(arr[0]);
Null
1
0.0
0
What happens if you try to compile and run the following code?
java
int[] nums = {1, 2, 3};
System.out.println(nums[3]);
The code will print 3.
The code will print 0.
The code will compile and run without errors.
The code will throw an ArrayIndexOutOfBoundsException.
What is the output of the following code snippet?
java
int[] numbers = new int[5];
numbers[0] = 10;
numbers[4] = 20;
System.out.println(numbers[2] + numbers[4]);
30
20
10
2
Consider the following code snippet. What is the output?
java
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] == 6) {
System.out.println(arr[i] + " " + arr[j]);
}
}
}
1 5\n2 4\n3 3\n4 2\n5 1
1 5
3 3
1 2 3 4 5
What exception is thrown when you try to access an array element with an index that is out of bounds?
NullPointerException
IllegalArgumentException
ArrayIndexOutOfBoundsException
IndexOutOfBoundsException
How do you access the first element of an array named values
in Java?
values[1]
values.first()
values[0]
values.get(0)
Given the array double[] prices = {19.99, 24.50, 17.75};
, how would you change the second element to 29.99
?
prices[1] = 29.99;
prices.set(1, 29.99);
prices[2] = 29.99;
prices[0] = 29.99;

How are we doing?
Give us your feedback and let us know how we can improve
What is the primary purpose of traversing an array?
To delete the array from memory.
To access each element in the array.
To change the size of the array.
To sort the array in ascending order.
Given an array int[] values = {5, 10, 15, 20};
, write a loop to add 2 to each element. Which of the following code snippets correctly does this?
for (int i = 0; i < values.length; i++) { values[i] += 2; }
for (int value : values) { value += 2; }
for (int i = 1; i <= values.length; i++) { values[i] = values[i] + 2; }
for (int i = 0; i < values.length; i++) { values = values[i] + 2; }
Which of the following is the correct way to declare an array of integers named numbers
in Java?
int numbers[];
array numbers = int[];
numbers int[];
int[] numbers;