Verityprep
Question Bank

AP® Computer Science A Multiple Choice Questions

Work through AP Computer Science A multiple-choice questions unit by unit, 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 971–980 of 2,811
  1. 971
  2. 972
  3. 973
  4. 974
  5. 975
  6. 976
  7. 977
  8. 978
  9. 979
  10. 980

Question 971

Multiple choice Unit 2: Selection and Iteration

What is printed?

int a = 7, b = 3, c = 5;
int result = 0;
if (a > b + c) {
    result = 1;
} else if (a > b && a > c) {
    if (b + c > a + 1) {
        result = 2;
    } else {
        result = 3;
    }
} else {
    result = 4;
}
System.out.println(result);
Pick an answer, then check it. The solution opens in a window.

Question 972

Multiple choice Unit 2: Selection and 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 973

Multiple choice Unit 2: Selection and 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 974

Multiple choice Unit 2: Selection and 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 975

Multiple choice Unit 2: Selection and 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 976

Multiple choice Unit 2: Selection and 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 977

Multiple choice Unit 2: Selection and 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 978

Multiple choice Unit 2: Selection and 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 979

Multiple choice Unit 2: Selection and 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 980

Multiple choice Unit 2: Selection and 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 971 of 2,811
Page 98 of 282