Verityprep
Question Bank · Unit 2

AP® Computer Science A Selection and Iteration Multiple Choice Questions

Work through AP Computer Science A multiple-choice 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 211–220 of 376
  1. 211
  2. 212
  3. 213
  4. 214
  5. 215
  6. 216
  7. 217
  8. 218
  9. 219
  10. 220

Question 211

Multiple choice Iteration
Consider the following code segment.
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(2);
nums.add(4);
nums.add(4);
nums.add(6);
nums.add(4);
for (int i = 0; i < nums.size(); i++)
{
    if (nums.get(i) == 4)
    {
        nums.remove(i);
    }
}
System.out.println(nums);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 212

Multiple choice Iteration
Consider the following method. public static int count(int n) { if (n < 1) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 213

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

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 214

Multiple choice Iteration
Consider the following code segment.
int n = 100;
int steps = 0;
while (n > 1)
{
    if (n % 2 == 0)
    {
        n = n / 2;
    }
    else
    {
        n = n - 1;
    }
    steps = steps + 1;
}
System.out.println(steps);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 215

Multiple choice Iteration
Consider the following code segment. String s = ""; for (int r = 1; r <= 3; r++) { for (int c = 0; c < r;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 216

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 217

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 218

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 219

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 220

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 211 of 376
Page 22 of 38