AP® Computer Science A Data Collections Practice Questions
Work through AP Computer Science A practice questions on Data Collections, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Question 1192
Question 1193
Question 1194
Question 1195
Question 1196
Question 1197
Consider the following recursive method.
public static int score(int[] arr, int k)
{
if (k >= arr.length)
{
return 0;
}
int rest = score(arr, k + 2);
if (arr[k] % 2 == 0)
{
return rest + arr[k];
}
return rest - arr[k];
}
What value is returned by the following code?
int[] values = {5, 2, 4, 7, 6};
score(values, 0);Main Solution
Answer
Answer: C. The method visits indices 0, 2, and 4. Index 4 contributes +6, index 2 contributes +4, and index 0 contributes -5. The total is 5.
Answer: Correct: C
A
15
B
10
C
5
D
-5
Answer images
Question 1198
Question 1199
A school system models course requests using the CourseRequest class. A request has a course name, a priority value, and a flag indicating whether the request has already been fulfilled.
public class CourseRequest
{
private String courseName;
private int priority;
private boolean fulfilled;
public CourseRequest(String name, int p, boolean done)
{
courseName = name;
priority = p;
fulfilled = done;
}
public String getCourseName()
{
return courseName;
}
public int getPriority()
{
return priority;
}
public boolean isFulfilled()
{
return fulfilled;
}
}
public class CourseQueue
{
private ArrayList<CourseRequest> requests;
public CourseQueue(ArrayList<CourseRequest> data)
{
requests = data;
}
public ArrayList<String> topOpenRequests(int minPriority)
{ /* to be written */ }
}
Write the method topOpenRequests, which returns a new ArrayList<String> containing the names of all unfulfilled requests whose priority is at least minPriority and is equal to the highest priority among all such requests. Names must appear in the same order as the corresponding requests appear in requests. If no request qualifies, the method returns an empty list.
Main Solution
Sample Solution
public ArrayList<String> topOpenRequests(int minPriority)
{
ArrayList<String> result = new ArrayList<String>();
int best = 0;
boolean found = false;
for (int i = 0; i < requests.size(); i++)
{
CourseRequest req = requests.get(i);
if (!req.isFulfilled() && req.getPriority() >= minPriority)
{
if (!found || req.getPriority() > best)
{
best = req.getPriority();
found = true;
}
}
}
if (!found)
{
return result;
}
for (int i = 0; i < requests.size(); i++)
{
CourseRequest req = requests.get(i);
if (!req.isFulfilled() && req.getPriority() == best)
{
result.add(req.getCourseName());
}
}
return result;
}
Scoring (9 pts): Points are earned for creating a result list, traversing all requests, using accessor methods, filtering by unfulfilled status and minimum priority, finding the best qualifying priority, handling no qualifying requests, collecting all ties in original order, adding course names rather than request objects, and returning the result.
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

