AP Computer Science A Practice Quiz: Implementing Array Algorithms
Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026
Test your understanding with short quizzes. This quiz has 7 questions to check your progress.
Question 1 of 7
All Questions (7)
A) 2
B) 10
C) 12
D) 15
Correct Answer: D
This is a standard algorithm to determine the maximum value in an array. The method initializes `maxVal` to the first element (10). It then traverses the rest of the array. When it encounters 15, which is greater than the current `maxVal` of 10, it updates `maxVal` to 15. No subsequent element is greater than 15, so 15 is returned.
A) allPositive(new int[]{-1, 2, 3, 4})
B) allPositive(new int[]{5, 8, 0, 2})
C) allPositive(new int[]{1, 10, 100, 50})
D) allPositive(new int[]{-5, -10, -15})
Correct Answer: C
This algorithm determines if all elements have a particular property (being positive). The method traverses the array and returns `false` immediately if it finds any element that is less than or equal to zero. If the loop completes without finding such an element, it returns `true`. Only the array `{1, 10, 100, 50}` consists entirely of positive numbers, so the call with this array will return `true`.
A) The number of elements that are smaller than the next element in the array.
B) The number of elements that are larger than the next element in the array.
C) The total number of elements in the array.
D) The number of duplicate elements in the array.
Correct Answer: A
This is an algorithm that accesses all consecutive pairs of elements. The loop iterates from the first element up to the second-to-last element. In each iteration, it compares an element `data[i]` with its consecutive neighbor `data[i+1]`. The counter `count` is incremented only when the current element is less than the next one. Therefore, the method counts the number of times an element is smaller than its immediate successor.
A) {5, 8, 2, 10, 4}
B) {8, 2, 10, 4, 5}
C) {4, 5, 8, 2, 10}
D) {8, 8, 2, 10, 4}
Correct Answer: B
This algorithm correctly performs a left shift (or rotation) on the elements. First, the value of the first element (5) is stored in the variable `first`. Then, the loop traverses the array from index 0 to the second-to-last index, copying each element from the position to its right (`arr[i] = arr[i+1]`). After the loop, the array is `{8, 2, 10, 4, 4}`. Finally, the stored `first` element (5) is placed at the last position. The final state of the array is `{8, 2, 10, 4, 5}`.
A) 2
B) 3
C) 4
D) 6
Correct Answer: B
This is a standard algorithm to determine the number of elements having a particular property. The method traverses the array `values`. For each element, it checks if the element is even using the modulo operator (`val % 2 == 0`). The `counter` is incremented for each even number found. In the array `{1, 2, 3, 4, 5, 6}`, the even numbers are 2, 4, and 6. Therefore, the counter will be incremented 3 times, and the method will return 3.
A) The method returns `true` if and only if all elements in the array are identical.
B) The method returns `true` if and only if the first and last elements are identical.
C) The method returns `true` if there is at least one pair of identical elements in the array.
D) The method returns `true` if and only if there are consecutive identical elements.
Correct Answer: C
This algorithm uses a nested loop to check for the presence of duplicate elements. The outer loop selects an element `list[i]`. The inner loop then compares `list[i]` to every subsequent element `list[j]` (since `j` starts at `i + 1`). If any such pair is found to be equal, the method immediately returns `true`. If the loops complete without finding any matching pair, it means no duplicates exist, and the method returns `false`. Therefore, it returns `true` if at least one duplicate value is present anywhere in the array.
A) {10, 20, 30, 40, 50}
B) {50, 40, 30, 20, 10}
C) {20, 30, 40, 50, 10}
D) {50, 10, 40, 20, 30}
Correct Answer: B
This is a standard algorithm to reverse the order of elements in an array. It uses two pointers, `left` starting at the beginning and `right` at the end. In each iteration of the `while` loop, it swaps the elements at the `left` and `right` indices. Then, `left` is incremented and `right` is decremented, moving the pointers toward the center. The loop continues until the pointers meet or cross. For `{10, 20, 30, 40, 50}`, it will perform the following swaps: 10 and 50, then 20 and 40. The middle element 30 remains in place. The final array is `{50, 40, 30, 20, 10}`.