Verityprep
Question Bank · Unit 2

AP® Computer Science A Selection and Iteration Practice Questions

Work through AP Computer Science A practice questions on Selection and Iteration, 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 431–440 of 740
  1. 431
  2. 432
  3. 433
  4. 434
  5. 435
  6. 436
  7. 437
  8. 438
  9. 439
  10. 440

Question 431

Multiple choice Boolean Expressions
Consider the following code segment, where arr is a non-null int array (possibly empty).
int i = 0;
while (i < arr.length && arr[i] != 0)
{
    i = i + 1;
}
Which of the following statements about the loop is true for every valid value of arr?
Pick an answer, then check it. The solution opens in a window.

Question 432

Multiple choice Iteration
Consider the following code segment. int[] a = {5, 2, 8, 1, 9, 3}; for (int i = 0; i < a.length - 1; i++) { if (a[i] >…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 433

Multiple choice Boolean Expressions
Consider the following code segment. ArrayList<Integer> list = new ArrayList<Integer>(); list.add(3); list.add(6); list.add(3); list.add(3); list.add(9);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 434

Multiple choice Iteration
A binary search is performed on the sorted array a = {3, 8, 14, 20, 27, 35, 44, 56, 71, 89} looking for the value 56. The search uses the standard recurrence…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 435

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

Question 436

Multiple choice Iteration
Consider the following code segment.
int total = 0;
for (int i = 1; i <= 6; i++)
{
    if (i % 2 == 0 || i < 3)
        total = total + i;
}
What value is stored in total after the loop finishes?
Pick an answer, then check it. The solution opens in a window.

Question 437

Multiple choice Iteration
Consider the following code segment. int x = 40; int count = 0; while (x > 3) { if (x % 2 ==…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 438

Multiple choice Boolean Expressions
Which of the following is equivalent to !(x > 5 && y != 2) ?
Pick an answer, then check it. The solution opens in a window.

Question 439

Multiple choice Iteration
Consider the following code segment. int sum = 0; for (int i = 0; i < 5; i++) { sum =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 440

Multiple choice Iteration
Consider the following method. public static int mystery(int n) { int count = 0; while (n > 0) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 431 of 740
Page 44 of 74