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,041–1,050 of 1,079
  1. 1041
  2. 1042
  3. 1043
  4. 1044
  5. 1045
  6. 1046
  7. 1047
  8. 1048
  9. 1049
  10. 1050

Question 1041

Multiple choice Using Text Files
A text file "scores.txt" contains the following two lines. 85 90 78 92 88 95 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 1042

Multiple choice Arrays
Consider the following code segment, where data is a non-empty int array containing only positive values terminated by a single -1 sentinel that appears…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1043

Multiple choice Arrays
Consider the following class. public class Stack3 { private int[] data; private int top; public Stack3() { data = new int[3]; top = 0; } public boolean push(int…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1044

Multiple choice 2D Arrays
Consider the following code segment.
int[][] g = { {1, 2, 3, 4},
               {5, 6, 7, 8},
               {9, 10, 11, 12} };
int sum = 0;
for (int r = 0; r < g.length; r++)
{
    for (int c = 0; c < g[0].length; c++)
    {
        if (r == c || c == g[0].length - 1 - r)
        {
            sum = sum + g[r][c];
        }
    }
}
System.out.println(sum);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 1045

Multiple choice Recursion
Consider the following method. public static int mystery(int n) { if (n <= 1) { return n;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1046

Multiple choice Sorting And Searching
Consider an int array a initially equal to {40, 18, 25, 7, 31, 12}. Selection sort, sorting in ascending order, repeatedly finds the…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1047

Multiple choice Arrays
Consider an int array a initially equal to {40, 12, 25, 7, 31, 18}. Insertion sort, sorting in ascending order, treats the prefix of length k as already sorted…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1048

Multiple choice Using Text Files
A text file contains the following exact contents (one record per line, comma-separated):
name,grade,present
Ali,87,true
Bea,72,false
Cyd,91,true
Dax,54,true
A program creates a properly constructed Scanner sc reading that file, then runs the following code. Assume any imports needed are present.
sc.nextLine();
int total = 0;
int n = 0;
while (sc.hasNext())
{
    String line = sc.nextLine();
    String[] parts = line.split(",");
    if (parts[2].equals("true"))
    {
        total = total + Integer.parseInt(parts[1]);
        n = n + 1;
    }
}
sc.close();
double avg = total * 1.0 / n;
What is the value of avg when the segment finishes?
Pick an answer, then check it. The solution opens in a window.

Question 1049

Multiple choice Arraylists
Consider the following class. public class Reading { private String sensor; private double value; public Reading(String s, double v) { sensor = s; value = v; }…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1050

Multiple choice Arrays
Consider the following code segment. int[] a = {4, 7, 2, 9, 5}; int sum = 0; for…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1,041 of 1,079
Page 105 of 108