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.

Subtopic: Arraylists Clear filter

Questions 181–190 of 225
  1. 181
  2. 182
  3. 183
  4. 184
  5. 185
  6. 186
  7. 187
  8. 188
  9. 189
  10. 190

Question 181

Free response Arraylists
A sensor produces a stream of numeric readings. Consider the following incomplete class: public class SensorLog { private ArrayList<Double> readings; public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 182

Multiple choice Arraylists
What does list contain after the following loop? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(3); list.add(1);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 183

Multiple choice Arraylists
What is printed? ArrayList<String> words = new ArrayList<String>(); words.add("cat"); words.add("car"); words.add("dog"); words.add("care"); words.add("card");…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 184

Free response Arraylists
The Arctic Research Station Polaris-7 collects hourly temperature readings from its sensor array. Scientists store these readings in an ArrayList<Double> and…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 185

Multiple choice Arraylists

What is printed?

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(4); list.add(4); list.add(3);
list.add(4); list.add(4);
for (int i = 0; i < list.size(); i++) {
    if (list.get(i) == 4) {
        list.remove(i);
    }
}
System.out.println(list);
Pick an answer, then check it. The solution opens in a window.

Question 186

Multiple choice Arraylists

What is printed?

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) { list.add(i * i); }
for (int i = 0; i < list.size() - 1; i++) {
    list.set(i, list.get(i) + list.get(i + 1));
}
System.out.println(list);
Pick an answer, then check it. The solution opens in a window.

Question 187

Free response Arraylists

An online community platform stores user posts as strings in an ArrayList. The moderation team needs tools to clean up duplicate content and identify trending topics. You are writing two analysis methods for the MessageBoard class.

PART A

Write the method public ArrayList<String> removeDuplicates(ArrayList<String> posts) that returns a new ArrayList containing only the first occurrence of each unique string, in original order.

For example, if posts is ["hello", "world", "hello", "java", "world", "hello"], the method returns ["hello", "world", "java"].

public ArrayList<String> removeDuplicates(ArrayList<String> posts) {

PART B

Write the method public String mostFrequent(ArrayList<String> posts) that returns the string appearing most often. If there is a tie, return the one that appears first in the list.

For example, if posts is ["cat", "dog", "cat", "bird", "dog", "cat"], the method returns "cat" (appears 3 times).

public String mostFrequent(ArrayList<String> posts) {
The answer and the full solution open in a window.

Question 188

Multiple choice Arraylists

What is printed?

ArrayList<String> words = new ArrayList<String>();
words.add("cat"); words.add("dog"); words.add("car");
words.add("dot"); words.add("cup");

ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < words.size(); i++) {
    String w = words.get(i);
    if (w.substring(0, 1).equals("c")) {
        result.add(w);
    }
}
for (int i = result.size() - 1; i >= 0; i--) {
    if (result.get(i).indexOf("a") != -1) {
        result.remove(i);
    }
}
System.out.println(result);
Pick an answer, then check it. The solution opens in a window.

Question 189

Multiple choice Arraylists

What is printed?

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++) {
    list.add(i * i);
}
// list = [1, 4, 9, 16, 25, 36]
for (int i = list.size() - 1; i >= 1; i -= 2) {
    list.set(i, list.get(i) - list.get(i - 1));
}
System.out.println(list);
Pick an answer, then check it. The solution opens in a window.

Question 190

Free response Arraylists

A neighborhood book club meets monthly to discuss and rate books. Members rate each book on a scale of 1 to 10, and the club maintains two parallel ArrayLists: one for book titles and one for their average ratings. The BookClub class provides methods to help members select their next read.

PART A

Write the method public ArrayList<String> getTopRated(ArrayList<String> titles, ArrayList<Integer> ratings, int threshold) that returns a new ArrayList containing only the titles whose corresponding rating is greater than or equal to threshold, preserving the original order.

For example, if titles is ["Dune", "1984", "Cosmos", "Beloved"] and ratings is [9, 7, 8, 6] with threshold 8, the method returns ["Dune", "Cosmos"].

public ArrayList<String> getTopRated(ArrayList<String> titles, ArrayList<Integer>
    ratings, int threshold) {

PART B

Write the method public String recommend(ArrayList<String> titles, ArrayList<Integer> ratings) that removes and returns the title with the highest rating. If there is a tie, remove and return the one appearing earliest in the list. Both the titles and ratings lists should have the corresponding element removed.

For example, if titles is ["Dune", "1984", "Cosmos"] and ratings is [9, 7, 9], the method returns "Dune" (first with highest rating 9) and both lists are reduced to ["1984", "Cosmos"] and [7, 9].

public String recommend(ArrayList<String> titles, ArrayList<Integer> ratings) {
The answer and the full solution open in a window.
Question 181 of 225
Page 19 of 23