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.

Subtopic: Iteration Clear filter

Questions 171–180 of 376
  1. 171
  2. 172
  3. 173
  4. 174
  5. 175
  6. 176
  7. 177
  8. 178
  9. 179
  10. 180

Question 171

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 172

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 173

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 174

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 175

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 176

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 177

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 178

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 179

Multiple choice Iteration
int x = (int)(Math.random() * 10); while (x < 5) { System.out.print(x + " "); x =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 180

Multiple choice Iteration
Consider the following code. int x = (int)(Math.random() * 5); while (x > 0) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 171 of 376
Page 18 of 38