AP Computer Science A Practice Test: Arrays and ArrayList
- Arrays and ArrayList
- 10 questions
- 15 minutes
Ten Java code-tracing questions on arrays and ArrayList. Tip: write down the contents of the array or list after every statement instead of tracing in your head.
Tap an option to mark your answer. You can change your mind, or tap the same option again to leave the question blank. The correct answers and explanations appear when you finish the test.Tap an option to answer; tap it again to leave the question blank.
Your result
- 0 correct
- 0 wrong
- 0 blank
-
Question 1
int[] a = {4, 8, 15, 16, 23};
int s = 0;
for (int i = 1; i < a.length; i += 2)
{
s += a[i];
}
System.out.println(s);What is printed?
Correct answer: C
The loop visits indices 1 and 3, adding 8 and 16. Summing indices 0, 2 and 4 would give 42, and summing everything would give 66.
-
Question 2
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5); list.add(10); list.add(15); list.add(20);
list.remove(1);
list.add(1, 7);
list.set(2, list.get(2) + 1);
System.out.println(list);What is printed?
Correct answer: A
remove(1) deletes the element at index 1 (10), giving [5, 15, 20]; add(1, 7) inserts to get [5, 7, 15, 20]; set(2, ...) raises 15 to 16.
-
Question 3
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(2); nums.add(4); nums.add(4); nums.add(6); nums.add(8);
for (int i = 0; i < nums.size(); i++)
{
if (nums.get(i) == 4)
{
nums.remove(i);
}
}
System.out.println(nums);What is printed?
Correct answer: B
After the first 4 at index 1 is removed, the second 4 shifts to index 1, but i moves on to 2 and never checks it. Removing during a forward loop skips an element.
-
Question 4
int[] arr = new int[5];
for (int i = 0; i <= arr.length; i++)
{
arr[i] = i * 2;
}What happens when this code runs?
Correct answer: D
The condition i <= arr.length allows i to equal 5, which is not a valid index for a five-element array. The first five assignments succeed before the exception occurs.
-
Question 5
int[] nums = {1, 2, 3};
for (int n : nums)
{
n = n * 2;
}
System.out.println(nums[0] + nums[1] + nums[2]);What is printed?
Correct answer: C
In an enhanced for loop, n is a copy of each element, so changing n does not change the array. The array still holds 1, 2 and 3.
-
Question 6
public static int mystery(int[] a)
{
int c = 0;
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1])
{
c++;
}
}
return c;
}What is returned by mystery(new int[] {3, 1, 4, 1, 5, 9, 2, 6})?
Correct answer: B
The method counts positions where an element is greater than the next one: 3>1, 4>1 and 9>2. That is three.
-
Question 7
int[] r = {1, 2, 3, 4};
for (int i = 0; i < r.length; i++)
{
int temp = r[i];
r[i] = r[r.length - 1 - i];
r[r.length - 1 - i] = temp;
}What are the contents of r after the loop?
Correct answer: A
Each pair is swapped twice because the loop runs over the whole array, not just the first half. The second pass undoes the first, so the array returns to its original order.
-
Question 8
ArrayList<String> w = new ArrayList<String>();
w.add("ant"); w.add("bee"); w.add("cat");
for (int i = 0; i < w.size(); i++)
{
w.set(i, w.get(i) + i);
}
System.out.println(w);What is printed?
Correct answer: D
Each element has its own index appended to the end: "ant" + 0, "bee" + 1, "cat" + 2. Concatenation puts the number after the string.
-
Question 9
Which of the following statements compiles without error in Java?
Correct answer: B
An ArrayList stores objects, so it needs the wrapper class Integer. The array statements are invalid because you cannot give both a size and an initializer list, and an array is created with square brackets, not parentheses.
-
Question 10
int[] p = {2, 5, 9};
int[] q = p;
q[0] = 100;
System.out.println(p[0]);What is printed?
Correct answer: C
The assignment q = p copies the reference, so p and q name the same array, and the change through q is visible through p. Making an independent copy would require a new array.
Questions you leave blank are not counted as wrong.