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 331–340 of 740
  1. 331
  2. 332
  3. 333
  4. 334
  5. 335
  6. 336
  7. 337
  8. 338
  9. 339
  10. 340

Question 331

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 332

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 333

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 334

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 335

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 336

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 337

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 338

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 339

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 340

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 331 of 740
Page 34 of 74