PrepGo

AP Computer Science A Practice Quiz: Array Traversals

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 10 questions to check your progress.

Question 1 of 10

According to the provided text, what is the term for using a repetition statement to access an ordered sequence of elements in an array?

All Questions (10)

According to the provided text, what is the term for using a repetition statement to access an ordered sequence of elements in an array?

A) Array Initialization

B) Array Traversal

C) Array Declaration

D) Array Assignment

Correct Answer: B

The text defines traversing an array as 'when repetition statements are used to access all or an ordered sequence of elements in an array.'

In an enhanced for loop, what is assigned to the loop's variable during each iteration?

A) The index of the current element

B) A reference to the current element

C) The memory address of the array

D) A copy of the current element's value

Correct Answer: D

The text states, 'For each iteration, the enhanced for loop variable is assigned a copy of an element without using its index.'

Consider the following code segment: int[] data = {10, 20, 30}; for (int value : data) { value = 5; } After this code executes, what will be the value of data[1]?

A) 5

B) 10

C) 20

D) 30

Correct Answer: C

The text explains that 'Assigning a new value to the enhanced for loop variable does not change the value stored in the array.' The variable 'value' is a copy, so modifying it does not affect the original 'data' array. Therefore, data[1] remains 20.

Based on the provided information, which of the following is a key characteristic of an enhanced for loop traversal?

A) It requires manual management of an index variable.

B) It is the most efficient way to modify all elements in an array.

C) It accesses elements without using their indices.

D) It can only be used to traverse an array backwards.

Correct Answer: C

The text explicitly mentions that in an enhanced for loop, 'the enhanced for loop variable is assigned a copy of an element without using its index.'

What is the result of the following code traversal? int[] nums = {2, 4, 6, 8}; int sum = 0; for (int n : nums) { if (n > 5) { sum += n; } } // What is the final value of sum?

A) 6

B) 12

C) 14

D) 20

Correct Answer: C

The code traverses the 'nums' array. The loop variable 'n' takes on the values 2, 4, 6, and 8. The 'if' condition (n > 5) is true for 6 and 8. The sum is calculated as 6 + 8, which equals 14. This demonstrates determining the result of a traversal.

Why does assigning a new value to the enhanced for loop variable NOT change the value stored in the array?

A) Because the loop variable is only a copy of the element's value.

B) Because arrays are always read-only during traversals.

C) Because the operation requires an explicit index, which is not available.

D) Because the compiler optimizes away the assignment statement.

Correct Answer: A

The provided text directly supports this by stating that the loop variable is assigned a 'copy of an element' and that assigning a new value to this variable 'does not change the value stored in the array.' The reason for this behavior is that the modification is happening to the copy, not the original element.

What is the primary programming construct used to perform an array traversal?

A) Conditional statements (if-else)

B) Repetition statements (loops)

C) Object instantiation (new)

D) Method declarations

Correct Answer: B

The text states, 'Traversing an array is when repetition statements are used to access all or an ordered sequence of elements in an array.'

A programmer wants to set every element in an integer array `grades` to 0. They write the following code: for (int g : grades) { g = 0; } Based on the provided text, will this code achieve the programmer's goal?

A) Yes, because the loop iterates through every element.

B) Yes, but only if the array was empty to begin with.

C) No, because assigning to 'g' only changes a temporary copy.

D) No, because an enhanced for loop cannot be used with primitive types like int.

Correct Answer: C

The provided content explicitly states that 'Assigning a new value to the enhanced for loop variable does not change the value stored in the array.' The variable 'g' is a copy, so setting it to 0 has no effect on the original 'grades' array.

Which of the following statements is a direct consequence of the fact that the enhanced for loop variable is a copy of an array element?

A) The enhanced for loop is slower than a traditional for loop.

B) The enhanced for loop cannot be used to modify the contents of the array.

C) The enhanced for loop can only access every other element.

D) The enhanced for loop requires more memory than a traditional for loop.

Correct Answer: B

Because the loop variable is a copy, any changes made to it are not reflected in the original array. Therefore, this type of loop cannot be used to directly modify the array's elements via assignment to the loop variable.

What is printed to the console after the following code is executed? String[] words = {"A", "B", "C"}; String result = ""; for (String s : words) { s = "X"; result += words[0]; } System.out.println(result);

A) XXX

B) ABC

C) AAA

D) CCC

Correct Answer: C

The traversal iterates three times. The line 's = "X"' modifies a copy and does NOT change the 'words' array. Inside the loop, 'result += words[0]' is executed three times. Since the 'words' array is never changed, 'words[0]' is always "A". Therefore, "A" is appended to 'result' three times, producing "AAA".