Verityprep
Question Bank

AP® Computer Science A Practice Questions

Work through AP Computer Science A practice 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 1,111–1,120 of 3,140
  1. 1111
  2. 1112
  3. 1113
  4. 1114
  5. 1115
  6. 1116
  7. 1117
  8. 1118
  9. 1119
  10. 1120

Question 1111

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 1112

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 1113

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 1114

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 1115

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 1116

Multiple choice Unit 2: Selection and Iteration

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 1117

Multiple choice Unit 2: Selection and Iteration

What is printed?

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

Question 1118

Multiple choice Unit 2: Selection and Iteration

What is printed?

int x = 12;
String label = "";
if (x % 2 == 0 && x % 3 == 0) {
    label = "A";
}
if (x % 4 == 0 && x % 3 == 0) {
    label = label + "B";
}
if (x % 6 == 0) {
    label = label + "C";
} else {
    label = label + "D";
}
System.out.println(label);
Pick an answer, then check it. The solution opens in a window.

Question 1119

Multiple choice Unit 2: Selection and Iteration

What is printed?

String s = "MISSISSIPPI";
int count = 0;
int pos = s.indexOf("SS");
while (pos != -1) {
    count++;
    s = s.substring(pos + 2);
    pos = s.indexOf("SS");
}
System.out.println(count + " " + s.length());
Pick an answer, then check it. The solution opens in a window.

Question 1120

Multiple choice Unit 2: Selection and Iteration

What is printed?

int a = 100, b = 73;
int count = 0;
while (a != b) {
    if (a > b) {
        a -= b;
    } else {
        b -= a;
    }
    count++;
}
System.out.println(a + " " + count);
Pick an answer, then check it. The solution opens in a window.
Question 1,111 of 3,140
Page 112 of 314