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.

Questions 381–390 of 692
  1. 381
  2. 382
  3. 383
  4. 384
  5. 385
  6. 386
  7. 387
  8. 388
  9. 389
  10. 390

Question 381

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 382

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 383

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 384

Multiple choice Boolean Expressions
Consider the following method.
public static String rate(int n)
{
    if (n < 0 || n > 100)
    {
        return "invalid";
    }
    if (n < 25)
    {
        return "low";
    }
    if (n < 75)
    {
        return "mid";
    }
    return "high";
}
For how many integer values of n in the range \(0 \leq n \leq 100\) does rate(n) return "mid"?
Pick an answer, then check it. The solution opens in a window.

Question 385

Multiple choice Boolean Expressions
Consider the following expression, where a and b are int variables. (a > 0 && b > 0) || (a < 0…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 386

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 387

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 388

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 389

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 390

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 381 of 692
Page 39 of 70