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.

Subtopic: Iteration Clear filter

Questions 161–170 of 383
  1. 161
  2. 162
  3. 163
  4. 164
  5. 165
  6. 166
  7. 167
  8. 168
  9. 169
  10. 170

Question 161

Multiple choice Iteration
What is printed? int x = 1; for (int i = 0; i <…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 162

Multiple choice Iteration

What is printed?

int product = 1;
for (int i = 1; i <= 6; i++) {
    if (i % 2 == 0) {
        product *= i;
    }
}
System.out.println(product);
Pick an answer, then check it. The solution opens in a window.

Question 163

Multiple choice Iteration

What is printed?

int sum = 0;
for (int i = 1; i <= 4; i++) {
    for (int j = i; j <= 4; j++) {
        sum += i * j;
    }
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 164

Multiple choice Iteration

What is printed?

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

Question 165

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

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

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

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

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

Multiple choice 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 161 of 383
Page 17 of 39