AP Computer Science A • Unit 4.4

Array Traversals

Detailed answer explanations for the Unit 4.4 multiple-choice practice questions.

Q1A
Q2A
Q3A
Q4C
Q5B
Q6C
Q7D
Q8B
Q9B
Q10A
Q11D
Q12B
Q13A
Q14A
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Which loop correctly traverses every valid index of a?

Afor (int i = 0; i < a.length; i++)
Bfor (int i = 0; i <= a.length; i++)
Cfor (int i = 1; i <= a.length; i++)
Dfor (int i = a.length; i > 0; i++)
Correct answer: A. A full indexed traversal starts at index 0 and continues while the index is less than array.length.
Explanation

The initial value, the condition, and the update together decide which indexes the loop visits. Starting at 0, stopping before a.length, and increasing by one visits every index from 0 through a.length - 1 exactly once.

  • B is not correct: The condition i <= a.length lets the index reach a.length itself, which is one position past the last valid index.
  • C is not correct: Starting at i = 1 skips the first element, and i <= a.length also runs one index past the end.
  • D is not correct: It begins at a.length, which is not a valid index, and the update i++ moves the index further away from 0 instead of toward it.
Q2

An indexed while traversal starts with int i = 0;. Which condition and update visit every element safely?

ACondition: i < a.length; update: i++
BCondition: i <= a.length; update: i++
CCondition: i > 0; update: i++
DCondition: i < a.length; no update
Correct answer: A. Continuing while i < a.length and increasing i each pass reproduces the equivalent while traversal shown in the section.
Explanation

A while traversal needs three pieces spread out: the index declared before the loop, a condition that stops at the last valid index, and an update inside the body. Because the index changes on each iteration, the loop walks the ordered sequence 0 through a.length - 1.

  • B is not correct: i <= a.length allows one extra pass in which a[i] reads one index past the end of the array.
  • C is not correct: With int i = 0; the test i > 0 is false right away, so the body never runs and no element is visited.
  • D is not correct: Without an update the index stays at 0 forever, so the condition never becomes false and the loop never advances through the array.
Q3

Which enhanced for loop header can read each int value in a?

Afor (int n : a)
Bfor (int n = a)
Cfor (int n < a.length)
Dfor (a : int n)
Correct answer: A. This matches the header form for (type var : array) that the section gives for enhanced for loops.
Explanation

An enhanced for header names an element type, a variable, a colon, and then the array. It hands you each element directly, which is why no index appears anywhere in the header.

  • B is not correct: It uses an assignment sign where the header form requires a colon between the loop variable and the array.
  • C is not correct: This is a comparison against a.length, which belongs to an indexed loop; the enhanced form names the array itself, not its length.
  • D is not correct: The array and the declared variable are in the opposite order; the type and variable come first and the array follows the colon.
Q4

What is a limitation of an enhanced for loop?

AIt starts at index 1.
BIt always changes array size.
CIt does not directly provide the current index.
DIt cannot read array values.
Correct answer: C. The enhanced for loop gives you each element directly, without an index, so the index is simply not available to you.
Explanation

That missing index is what decides between the two forms. Use the enhanced loop when every element is needed in forward order and its index is not; use an indexed loop when the index, a partial range, reverse order, or element replacement matters.

  • A is not correct: It visits every element in forward order, so no element is skipped; starting past the first element is what an indexed loop that begins at i = 1 does.
  • B is not correct: The loop only visits the elements already stored; it does not even replace one, since assigning to its variable changes only the copy.
  • D is not correct: Reading is exactly what it does well, as in the loop that prints each value of urgencies with System.out.println(u).
Q5

Which traversal is appropriate when you need to replace selected array elements?

AAn enhanced for loop that reassigns its loop variable
BAn index-based loop
CA loop that only prints selected values
DA loop that never assigns to a[i]
Correct answer: B. Replacing an element means assigning to a[i], and only an index-based loop supplies the index needed to name that element.
Explanation

The indexed fix in the section writes points[i] = points[i] + 1; inside an indexed for loop. The bracketed index reaches the stored element itself, so the new value goes into the array rather than into a copy.

  • A is not correct: The enhanced loop variable receives a copy of an element, so assigning to it changes only that copy and leaves the array unchanged.
  • C is not correct: Printing reads values and reports them; nothing is stored back, so no element is replaced.
  • D is not correct: A loop that never assigns to a[i] never writes a new value into any position, which is exactly what replacement requires.
Q6

Consider the following code. What is printed?

int[] fares = {3, 5, 7};
for (int fare : fares) {
    fare = 0;
}
System.out.println(fares[1]);
A0
B3
C5
D7
Correct answer: C. The array still holds {3, 5, 7} after the loop, so fares[1] prints the original 5.
Explanation

On each iteration the enhanced for variable fare receives a copy of one element. Assigning 0 to that variable changes the copied value only, the same way changing point left points[0] at 4.

  • A is not correct: 0 would be printed only if the assignment reached the stored element, but it reaches the copy held in fare.
  • B is not correct: 3 is the element at index 0, and the print statement asks for index 1.
  • D is not correct: 7 is the element at index 2, the last position, not the one the print statement names.
Q7

What is printed by this reverse traversal?

int[] a = {4, 7, 9};
for (int i = a.length - 1; i >= 0; i--) {
    System.out.print(a[i] + " ");
}
A4 7 9
B7 9
C9 7
D9 7 4
Correct answer: D. The loop starts at the last valid index and decreases, so the elements come out from last to first.
Explanation

The initial value a.length - 1 names the final position, the condition i >= 0 keeps going down to the first position, and i-- steps backward. Together they produce the ordered index sequence 2, 1, 0.

  • A is not correct: That is forward order, which would come from starting at 0 and increasing the index instead.
  • B is not correct: Only two values appear, so this leaves out an element; the condition i >= 0 keeps the loop running through the first position.
  • C is not correct: This stops after two values, but i >= 0 allows index 0, so the first element is printed as well.
Q8

What happens when this loop runs?

int[] a = {2, 4, 6};
for (int i = 0; i <= a.length; i++) {
    System.out.println(a[i]);
}
AIt prints all three values and stops normally.
BIt eventually causes an out-of-bounds error.
CIt skips the first element.
DIt does not compile because arrays have no length.
Correct answer: B. The condition i <= a.length lets the index reach 3, and a[3] is one position past the last valid index 2.
Explanation

The section warns directly against writing i <= arr.length when accessing arr[i]. The last valid index is array.length - 1, so a condition that admits array.length itself attempts an out-of-bounds access.

  • A is not correct: The three values are printed, but the loop does not stop there; it makes one more pass and tries to read past the end.
  • C is not correct: The index starts at 0, so the first element is printed; skipping it would require starting at i = 1.
  • D is not correct: Arrays do have length; the section uses array.length throughout its traversal conditions.
Q9

Consider the following code segment. What is printed?

int[] data = {3, 6, 9};
int i = 0;

while (i < data.length) {
    System.out.print(data[i] + " ");
    i++;
}
A9 6 3
B3 6 9
C0 1 2
DNothing; the loop condition is initially false.
Correct answer: B. This is the indexed while traversal from the section, so it prints the elements in forward order separated by spaces.
Explanation

The index is declared before the loop, tested against data.length, and increased inside the body. That gives the index sequence 0, 1, 2, and data[i] prints the element stored at each of those positions.

  • A is not correct: Reverse order would require starting at the last valid index and decreasing, not starting at 0 and using i++.
  • C is not correct: Those are the index values; the print statement uses data[i], which reaches the element rather than the index.
  • D is not correct: With int i = 0; and a length of 3, the test i < data.length is true at the start, so the body does run.
Q10

Consider the following code segment. Which indexed traversal is equivalent to the segment?

int[] a = {4, 6, 8};

for (int n : a) {
    System.out.println(n);
}
AStart at 0; continue while i < a.length; print a[i].
BStart at 1; continue while i < a.length; print a[i].
CStart at 0; continue while i <= a.length; print a[i].
DStart at 0; continue while i < a.length; print i.
Correct answer: A. This is the indexed rewrite the section shows: start at 0, continue while i < a.length, and print a[i].
Explanation

An enhanced for loop visits every element in forward order. An indexed loop matches it only when its bounds produce the index sequence 0 through a.length - 1 and its body prints the element at each index.

  • B is not correct: Starting at 1 skips the first element, while the enhanced loop visits every element including the one at index 0.
  • C is not correct: i <= a.length makes one extra pass and attempts an access one index past the end, which the enhanced loop never does.
  • D is not correct: Printing i reports the index numbers, but the enhanced loop hands over elements, so the printed values must be a[i].
Q11

Consider the following code segment. What is printed?

int[] f = {4, 2, 5, 3, 1};

for (int i = 2; i < f.length; i++) {
    f[i] = f[i] + 10;
}

System.out.println(f[1] + " " + f[2]);
A2 5
B12 15
C2 13
D2 15
Correct answer: D. The loop starts at index 2, so f[1] keeps its original 2 while f[2] becomes 5 + 10.
Explanation

The initial value of the index decides where changing begins. Because the loop runs from index 2 to the last valid index, only those elements are increased by 10, and the assignment f[i] = f[i] + 10; writes into the array itself.

  • A is not correct: These are the starting values; the assignment does store a new value at index 2, so f[2] no longer holds 5.
  • B is not correct: This adds 10 at index 1 as well, but the loop starts at i = 2, so index 1 is never visited.
  • C is not correct: 13 comes from adding 10 to 3, which is the element at index 3 rather than at index 2.
Q12

Consider the following code segment. Which elements of v are changed?

int[] v = {6, 2, 1, 7, 12};

for (int i = 0; i < v.length; i += 2) {
    v[i] = 0;
}
Athe elements at indexes 0, 1, and 2
Bthe elements at indexes 0, 2, and 4
Cthe elements at indexes 1, 3, and 5
Devery element of the array
Correct answer: B. The update i += 2 produces the index sequence 0, 2, 4, and those are the positions the assignment writes to.
Explanation

The update determines the ordered sequence of indexes just as the initial value and condition do. Increasing by two from 0 skips every other position while the condition i < v.length keeps every visited index valid.

  • A is not correct: Visiting three consecutive positions would need i++; increasing by two never lands on index 1.
  • C is not correct: The sequence starts at 0, not 1, and index 5 would be past the last valid index of this five-element array.
  • D is not correct: Every element would be changed only with an update of i++; here the odd-numbered positions are stepped over.
Q13

Assume Cat has a constructor and a setAge method. Consider the following code segment. What is the effect of the loop?

Cat[] c = {new Cat(4), new Cat(2)};

for (Cat cat : c) {
    cat.setAge(5);
}
ABoth objects in the array now have age 5.
BNeither object changes, because the loop variable holds a copy of the value.
CThe two array elements are replaced by two new objects.
DThe code does not compile, because the loop variable cannot call a method.
Correct answer: A. For an object array the copied element is a reference, so calling the mutator through it changes the object the array element reaches.
Explanation

The section shows the same pattern with Player: setScore is called through the loop variable and the printed score changes. The reference stored in the array is untouched, but the state of the object it reaches is not.

  • B is not correct: For an object array the copy is a reference, and a copied reference reaches the very same object, so a mutator call does take effect.
  • C is not correct: The references stored in the array stay exactly as they were; only the state inside the existing objects is changed.
  • D is not correct: Calling a method through the loop variable is allowed, as the Player example does with setScore and getScore.
Q14

Assume the Hat class has a constructor and a getSize method. Consider the following code segment. What is printed?

Hat[] h = {new Hat(1), new Hat(2)};

for (Hat hat : h) {
    hat = new Hat(9);
}

System.out.println(h[0].getSize());
A1
B2
C9
DNothing; assigning to hat does not compile.
Correct answer: A. Reassigning the loop variable does not replace an array element, so h[0] still reaches the object created with new Hat(1).
Explanation

The enhanced for variable holds a copy of the element, a reference in the case of an object array. Pointing that copy at a different object leaves the reference stored in the array unchanged, which is why a mutator call is needed to affect an object.

  • B is not correct: 2 belongs to the second element of the array, and the print statement reads index 0.
  • C is not correct: 9 would require the array element itself to be replaced, but the assignment reaches only the copied reference.
  • D is not correct: The assignment compiles; it simply has no effect on the array, exactly as assigning to point had none on points.