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 301–310 of 692
  1. 301
  2. 302
  3. 303
  4. 304
  5. 305
  6. 306
  7. 307
  8. 308
  9. 309
  10. 310

Question 301

Multiple choice 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 302

Multiple choice Boolean Expressions

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 303

Multiple choice 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 304

Multiple choice 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 305

Multiple choice Boolean Expressions

What is printed?

int score = 78;
String grade = "";
if (score >= 90) {
    grade = "A";
} else if (score >= 80) {
    grade = "B";
} else if (score >= 70) {
    grade = "C";
} else {
    grade = "F";
}
int bonus = score % 10;
if (bonus >= 5) {
    grade = grade + "+";
}
System.out.println(grade + " " + bonus);
Pick an answer, then check it. The solution opens in a window.

Question 306

Multiple choice Iteration

What is printed?

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

Question 307

Multiple choice Boolean Expressions
What is printed?

int x = 5, y = 10; boolean p = !(x > 3 && y < 15); boolean q = x <= 3 || y >= 15; boolean r = !(x > 3) || !(y < 15); System.out.println(p + " " + (p == q) + " " + (q == r));
Pick an answer, then check it. The solution opens in a window.

Question 308

Multiple choice Iteration

What is printed?

String s = "ABCDEF";
String result = "";
for (int i = 0; i < s.length(); i += 2) {
    result = s.substring(i, i + 1) + result;
}
System.out.println(result);
Pick an answer, then check it. The solution opens in a window.

Question 309

Multiple choice Iteration

What is printed?

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

Question 310

Multiple choice Iteration

What is printed?

int count = 0;
for (int i = 1; i <= 20; i++) {
    boolean divisible = false;
    for (int j = 2; j * j <= i; j++) {
        if (i % j == 0) {
            divisible = true;
        }
    }
    if (!divisible && i > 1) {
        count++;
    }
}
System.out.println(count);
Pick an answer, then check it. The solution opens in a window.
Question 301 of 692
Page 31 of 70