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 341–350 of 740
  1. 341
  2. 342
  3. 343
  4. 344
  5. 345
  6. 346
  7. 347
  8. 348
  9. 349
  10. 350

Question 341

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 342

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 343

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 344

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 345

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 346

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 347

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 348

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 349

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 350

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 341 of 740
Page 35 of 74