Verityprep
Question Bank · Unit 4

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.

Questions 1,171–1,180 of 1,222
  1. 1171
  2. 1172
  3. 1173
  4. 1174
  5. 1175
  6. 1176
  7. 1177
  8. 1178
  9. 1179
  10. 1180

Question 1171

Multiple choice Arraylists
Consider the following code segment. ArrayList<String> list = new ArrayList<String>(); list.add("A");…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1172

Multiple choice 2D Arrays
Consider the following code segment. int[][] m = { {1, 2, 3}, {4, 5, 6} }; int[][] r = new int[m[0].length][m.length]; for (int i = 0; i <…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1173

Multiple choice Recursion
Consider the following method.
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) ?
Pick an answer, then check it. The solution opens in a window.

Question 1174

Multiple choice Arrays
Consider the following code segment. int[] a = {7, 3, 9, 1, 5}; for (int i = 1; i < 4; i++) { int key = a[i]; int j = i - 1;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1175

Free response Arraylists

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.

The answer and the full solution open in a window.

Question 1176

Free response 2D Arrays
The TerrainMap class stores a rectangular grid of elevation values in meters. public class TerrainMap { private int[][] height; public TerrainMap(int[][] h) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1177

Multiple choice Arrays
Consider the following code segment. int[] nums = {4, 1, 6, 3, 8}; int total = 0; for (int i = 0; i < nums.length - 1; i++) { if…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1178

Multiple choice Arraylists
Consider the following code segment.
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);
What is printed?
Pick an answer, then check it. The solution opens in a window.

Question 1179

Multiple choice 2D Arrays
Consider the following code segment. int[][] m = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int sum = 0; for (int r = 0; r < m.length;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1180

Multiple choice Recursion
Consider the following method. public static int g(int n) { if (n ==…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1,171 of 1,222
Page 118 of 123