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 1172
Question 1173
public static int g(int n)
{
if (n == 1)
return 1;
if (n % 2 == 0)
return g(n / 2);
return 1 + g(n - 1);
}
What value is returned by g(12) ?Main Solution
Answer
Answer: A. Since 12 is even, g(12) = g(6). Since 6 is even, g(6) = g(3). Then g(3) = 1 + g(2) = 1 + g(1) = 2.
Answer: Correct: A
A
2
B
3
C
4
D
1
Answer images
Question 1174
Question 1175
Information about applicants for a scholarship program is stored in Applicant objects.
public class Applicant
{
private String name;
private int score;
private boolean complete;
public Applicant(String n, int s, boolean c)
{
name = n;
score = s;
complete = c;
}
public String getName()
{
return name;
}
public int getScore()
{
return score;
}
public boolean isComplete()
{
return complete;
}
}
The ScholarshipGroup class stores a list of applicants.
public class ScholarshipGroup
{
private ArrayList<Applicant> applicants;
public ScholarshipGroup(ArrayList<Applicant> list)
{
applicants = list;
}
public ArrayList<String> finalists(int minScore)
{ /* to be written */ }
}
Write the method finalists, which returns a new ArrayList<String> containing the names of all applicants who satisfy both of the following conditions.
- The applicant's form is complete.
- The applicant's score is at least
minScore.
Among the applicants who satisfy those conditions, only applicants whose score is equal to the highest qualifying score should appear in the returned list. Names in the returned list must appear in the same order as the corresponding applicants appear in applicants. If no applicant qualifies, the method returns an empty list.
For example, if the qualifying applicants have scores 81, 94, 94, 88, the returned list should contain the names of the two applicants whose score is 94, in their original order.
Answer images
Question 1176
Question 1177
Question 1178
ArrayList<Integer> values = new ArrayList<Integer>();
values.add(2);
values.add(5);
values.add(1);
values.add(5);
for (int i = values.size() - 1; i >= 0; i--)
{
if (values.get(i) == 5)
values.remove(i);
}
System.out.println(values);
Main Solution
Answer
Answer: D. The loop moves backward through the list, removing both occurrences of 5 without skipping any elements. The remaining values are 2 and 1.
Answer: Correct: D
A
[2, 5, 1]
B
[2, 1, 5]
C
[2]
D
[2, 1]
Answer images
Question 1179
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.

