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 211–220 of 383
  1. 211
  2. 212
  3. 213
  4. 214
  5. 215
  6. 216
  7. 217
  8. 218
  9. 219
  10. 220

Question 211

Multiple choice Iteration
Consider the following code segment. int total = 0; for (int k = 1; k <= 20; k++) { if (k % 2 == 0 && k %…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 212

Multiple choice Iteration
Consider the following code segment.
int n = 128;
int steps = 0;
while (n > 1)
{
    n = n / 2;
    steps = steps + 1;
}
System.out.println(steps);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 213

Multiple choice Iteration
Consider the following code segment. int count = 0; for (int i = 1; i <= 4; i++) { for (int j = i; j…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 214

Multiple choice Iteration
Consider the following code segment. int n = 1; int k = 0; while (n < 50) { n = n * 2; k =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 215

Multiple choice Iteration
Consider the following code segment. int[] arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 216

Multiple choice Iteration
Consider the following code segment.
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(2);
nums.add(4);
nums.add(4);
nums.add(6);
nums.add(4);
for (int i = 0; i < nums.size(); i++)
{
    if (nums.get(i) == 4)
    {
        nums.remove(i);
    }
}
System.out.println(nums);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 217

Multiple choice Iteration
Consider the following method. public static int count(int n) { if (n < 1) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 218

Multiple choice Iteration
Consider the following code segment. int sum = 0; for (int i = 5; i <= 20; i = i + 3) { if (i % 2…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 219

Multiple choice Iteration
Consider the following code segment.
int n = 100;
int steps = 0;
while (n > 1)
{
    if (n % 2 == 0)
    {
        n = n / 2;
    }
    else
    {
        n = n - 1;
    }
    steps = steps + 1;
}
System.out.println(steps);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 220

Multiple choice Iteration
Consider the following code segment. String s = ""; for (int r = 1; r <= 3; r++) { for (int c = 0; c < r;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 211 of 383
Page 22 of 39