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,131–1,140 of 1,222
  1. 1131
  2. 1132
  3. 1133
  4. 1134
  5. 1135
  6. 1136
  7. 1137
  8. 1138
  9. 1139
  10. 1140

Question 1131

Multiple choice Arraylists
Consider the following code segment.
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "X");
list.add(3, "Y");
list.remove(0);
System.out.println(list);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 1132

Free response Arraylists

A linguist is studying word usage in a corpus. Given an ArrayList<String> of the words that appear in a document (in order, with possible repetitions), the linguist wants the list of most common words in that document.

Write a static method mostFrequent that, given such a list, returns a new ArrayList<String> containing every word whose number of occurrences in words equals the maximum number of occurrences attained by any word. In the returned list:

  • each qualifying word appears exactly once, even if it occurs many times in words;
  • the qualifying words appear in the order of their first occurrence in words.

Two words are considered the same only if they are equal as strings (case-sensitive). The parameter words must not be modified. If words is empty, return an empty list.

Method Signature

public static ArrayList<String> mostFrequent(ArrayList<String> words)

Examples

Input words Returned list Explanation
["to","be","or","not","to","be"] ["to","be"] Both appear twice; "to" occurs first, then "be".
["red","blue","red","green","blue","red"] ["red"] Max count is 3 ("red"); "blue" has count 2.
["a","b","c","d"] ["a","b","c","d"] All words tie at count 1; order is preserved.
["Cat","cat","Cat"] ["Cat"] Case-sensitive: "Cat" has count 2, "cat" has count 1.
[] [] Empty input → empty output.
The answer and the full solution open in a window.

Question 1133

Free response 2D Arrays

An image-processing routine applies a smoothing filter to a rectangular grid of integer pixel values. For each cell in the input grid, the smoothed grid contains the integer average (truncated toward zero, as with Java's int division) of that cell and its orthogonal neighbors — the cells directly above, below, to the left, and to the right, when they exist.

A corner cell therefore averages three values (itself and its two in-bounds orthogonal neighbors); an edge cell averages four; and an interior cell averages five. Diagonal neighbors are never included. The original grid must not be modified.

Write a static method gridSmooth that takes a rectangular 2-D array grid of non-negative integers (at least one row and one column; every row has the same length) and returns a new 2-D array of the same dimensions containing the smoothed values, as defined above.

Method Signature

public static int[][] gridSmooth(int[][] grid)

Example 1

Given the 3×4 grid on the left, the result is shown on the right. Each cell shows the full computation for reference.

Input gridReturned grid
{ { 4, 8, 12, 0 },
  { 2, 6, 3, 5 },
  { 10, 1, 7, 9 } }
{ { 4, 7, 5, 5 },
  { 5, 4, 6, 4 },
  { 4, 6, 5, 7 } }

For instance, the value at row 1, column 1 is \((6 + 8 + 2 + 3 + 1) / 5 = 20 / 5 = 4\) (itself, up, left, right, down — truncated). The value at row 0, column 0 is \((4 + 8 + 2) / 3 = 14 / 3 = 4\) (itself, right, down — truncated). The value at row 2, column 3 is \((9 + 5 + 7) / 3 = 21 / 3 = 7\).

Example 2

A 1×1 grid maps to itself: gridSmooth({{ 42 }}) returns {{ 42 }} (the sum is 42, divided by 1).

A 1×3 grid behaves as if every cell is on the top and bottom edge: gridSmooth({{ 3, 6, 9 }}) returns {{ 4, 6, 7 }}, since \((3+6)/2 = 4\), \((6+3+9)/3 = 6\), and \((9+6)/2 = 7\).

The answer and the full solution open in a window.

Question 1134

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

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1135

Multiple choice Arraylists
Consider the following code segment. ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1136

Multiple choice Arrays
Consider the following code segment. int[] arr = {3, 7, 2, 9, 5, 1, 9, 4}; int best = arr[0]; int idx = 0; for (int i = 1; i < arr.length;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1137

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; r++) { for (int c = 0; c <…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1138

Multiple choice Arraylists
A method is intended to insert target into an ArrayList<Integer> called nums immediately after the first element that is strictly greater than target. If no…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1139

Multiple choice 2D Arrays
Consider the following method.
public static int f(int n)
{
    if (n <= 1)
    {
        return n;
    }
    return n + f(n - 2);
}
What is returned by the call f(7)?
Pick an answer, then check it. The solution opens in a window.

Question 1140

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

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1,131 of 1,222
Page 114 of 123