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.

Subtopic: Iteration Clear filter

Questions 221–230 of 383
  1. 221
  2. 222
  3. 223
  4. 224
  5. 225
  6. 226
  7. 227
  8. 228
  9. 229
  10. 230

Question 221

Multiple choice Iteration
Consider the following code segment. int p = 1; for (int i = 1; i <= 5; i++) { if (i % 2 ==…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 222

Multiple choice Iteration
Consider the following code segment. int n = 1; int total = 0; while (n < 100) { total = total + n; if (n < 10)…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 223

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 224

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 225

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 226

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 227

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 228

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 229

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 230

Multiple choice Iteration
Consider the following code segment.
int count = 0;
for (int i = 1; i <= 4; i++)
{
    for (int j = i; j <= 4; j++)
        count++;
}
What is the value of count after the code segment executes?
Pick an answer, then check it. The solution opens in a window.
Question 221 of 383
Page 23 of 39