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 291–300 of 692
  1. 291
  2. 292
  3. 293
  4. 294
  5. 295
  6. 296
  7. 297
  8. 298
  9. 299
  10. 300

Question 291

Multiple choice Iteration

What is printed?

int n = 2;
int count = 0;
while (n < 1000) {
    n = n * n;
    count++;
}
System.out.println(n + " " + count);
Pick an answer, then check it. The solution opens in a window.

Question 292

Multiple choice Iteration

What is printed?

int n = 4821;
int reversed = 0;
while (n > 0) {
    reversed = reversed * 10 + n % 10;
    n = n / 10;
}
System.out.println(reversed);
Pick an answer, then check it. The solution opens in a window.

Question 293

Multiple choice Iteration

What is printed?

int total = 0;
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 5; j++) {
        if (i + j == 6) {
            total += i * j;
        }
    }
}
System.out.println(total);
Pick an answer, then check it. The solution opens in a window.

Question 294

Multiple choice Iteration

What is printed?

String s = "ABCDEFGHIJ";
String result = "";
for (int i = s.length() - 1; i >= 0; i--) {
    if (i % 3 == 0) {
        result += s.substring(i, i + 1);
    }
}
System.out.println(result);
Pick an answer, then check it. The solution opens in a window.

Question 295

Multiple choice Iteration

Consider the following method.

public static int classify(int n) {
    int sumDiv = 0;
    for (int i = 1; i < n; i++) {
        if (n % i == 0)
            sumDiv += i;
    }
    if (sumDiv == n) return 0;
    if (sumDiv > n) return 1;
    return -1;
}

What is the return value of classify(28) ?

Pick an answer, then check it. The solution opens in a window.

Question 296

Multiple choice Iteration

What is printed?

int a = 48, b = 18;
while (b != 0) {
    int temp = b;
    b = a % b;
    a = temp;
}
System.out.println(a);
Pick an answer, then check it. The solution opens in a window.

Question 297

Multiple choice Iteration

What is printed?

int sum = 0;
for (int i = 1; i <= 30; i++) {
    if (i % 3 == 0 && i % 2 != 0) {
        sum += i;
    }
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 298

Multiple choice Iteration

What is printed?

int sum = 0;
for (int i = 1; i <= 4; i++) {
    for (int j = i; j <= 4; j++) {
        sum += j;
    }
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 299

Multiple choice Iteration

What is printed?

int result = 1;
for (int i = 1; i <= 5; i++) {
    if (i % 2 == 1) {
        result *= i;
    } else {
        result += i;
    }
}
System.out.println(result);
Pick an answer, then check it. The solution opens in a window.

Question 300

Multiple choice Boolean Expressions

What is printed?

int a = 0, b = 5, c = 3;
boolean p = (a != 0) && (b / a > 1);
boolean q = (a == 0) || (b / a > 1);
boolean r = (c > b) || (a != 0) && (b > c);
System.out.println(p + " " + q + " " + r);
Pick an answer, then check it. The solution opens in a window.
Question 291 of 692
Page 30 of 70