Verityprep
Question Bank

AP® Computer Science A Multiple Choice Questions

Work through AP Computer Science A multiple-choice questions unit by unit, 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 2,771–2,780 of 2,811
  1. 2771
  2. 2772
  3. 2773
  4. 2774
  5. 2775
  6. 2776
  7. 2777
  8. 2778
  9. 2779
  10. 2780

Question 2771

Multiple choice Unit 4: Data Collections
Consider the sorted array {2, 4, 7, 11, 15, 20, 26, 33, 41} (indices 0 through 8). A binary search is performed for the target value 20. The search uses the…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2772

Multiple choice Unit 4: Data Collections
A text file "items.txt" contains the following lines, one per line. apple,3 banana,5 cherry,2 Assume the file is opened successfully and the java.util.Scanner…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2773

Multiple choice Unit 4: Data Collections
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 2774

Multiple choice Unit 4: Data Collections
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 2775

Multiple choice Unit 4: Data Collections
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 2776

Multiple choice Unit 4: Data Collections
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 2777

Multiple choice Unit 4: Data Collections
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 2778

Multiple choice Unit 4: Data Collections
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 2779

Multiple choice Unit 4: Data Collections
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 2780

Multiple choice Unit 4: Data Collections
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 2,771 of 2,811
Page 278 of 282