Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections Multiple Choice Questions

Work through AP Computer Science A multiple-choice 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,071–1,079 of 1,079
  1. 1071
  2. 1072
  3. 1073
  4. 1074
  5. 1075
  6. 1076
  7. 1077
  8. 1078
  9. 1079

Question 1071

Multiple choice Arraylists
Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(2); nums.add(4); nums.add(3); nums.add(5);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1072

Multiple choice 2D Arrays

Consider the following code segment.

int[][] grid = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

int sum = 0;

for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[0].length; c++)
    {
        if (r + c == 2)
        {
            sum = sum + grid[r][c];
        }
        else if (r == c)
        {
            sum = sum - grid[r][c];
        }
    }
}

What is the value of sum after the loops?

Pick an answer, then check it. The solution opens in a window.

Question 1073

Multiple choice Recursion
Consider the following recursive method. public static int recur(int n) { if (n < 3) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1074

Multiple choice Sorting And Searching
Selection sort is used on the array {6, 2, 8, 1,…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1075

Multiple choice Sorting And Searching
Insertion sort is used on the array {9, 4, 7, 3, 8}. What is…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1076

Multiple choice Sorting And Searching
A binary search is performed on the sorted array {2, 5, 9, 14, 20, 27, 31, 40} for target 27. The midpoint is computed…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1077

Multiple choice Using Text Files
A text file "orders.txt" contains the following data. red 3 blue 5 green 2 Assume the file is opened successfully and the java.util.Scanner and java.io.File…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1078

Multiple choice Using Text Files
A text file "scores.txt" contains the following lines. Ava,7,true Bo,9,false Cy,10,true Assume the file is opened successfully and the java.util.Scanner and…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1079

Multiple choice Recursion

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);
Pick an answer, then check it. The solution opens in a window.
Question 1,071 of 1,079
Page 108 of 108