PrepGo

AP Computer Science A Practice Quiz: Nested Iteration

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 7 questions to check your progress.

Question 1 of 7

Consider the following code segment. int count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { count++; } } What is the final value of `count` after this code segment executes?

All Questions (7)

Consider the following code segment. int count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { count++; } } What is the final value of `count` after this code segment executes?

A) 3

B) 5

C) 8

D) 15

Correct Answer: D

The outer loop runs 3 times (for i = 0, 1, 2). For each single iteration of the outer loop, the inner loop runs 5 times (for j = 0, 1, 2, 3, 4). Therefore, the statement `count++` is executed a total of 3 * 5 = 15 times.

Which statement best describes the execution of a nested loop? for (int outer = 1; outer <= 2; outer++) { for (int inner = 1; inner <= 3; inner++) { // some action } }

A) The outer loop runs 2 times, and then the inner loop runs 3 times, for a total of 5 iterations.

B) The inner loop runs 3 times, and then the outer loop runs 2 times, for a total of 5 iterations.

C) For each of the outer loop's 2 iterations, the inner loop completes all 3 of its iterations.

D) The outer loop and inner loop alternate, with the outer loop running once, then the inner loop once, and so on.

Correct Answer: C

The definition of nested iteration states that the inner loop must complete all its iterations before the outer loop can continue to its next iteration. Therefore, for `outer = 1`, the inner loop runs 3 times. Then, for `outer = 2`, the inner loop runs another 3 times.

Consider the following code segment. for (int row = 1; row <= 2; row++) { for (int col = 1; col <= 3; col++) { System.out.print(row + ","); } } What is printed to the console?

A) 1,2,3,1,2,3,

B) 1,1,1,2,2,2,

C) 1,2,1,2,1,2,

D) 1,2,

Correct Answer: B

The outer loop variable `row` starts at 1. The inner loop then completes all its iterations, printing the value of `row` (which is 1) three times. Then, `row` increments to 2. The inner loop runs again, this time printing the new value of `row` (which is 2) three times. The final output is 1,1,1,2,2,2,.

Consider the following code segment. int total = 0; for (int x = 1; x <= 4; x++) { for (int y = 1; y < x; y++) { total++; } } What is the final value of `total` after this code segment executes?

A) 4

B) 6

C) 10

D) 16

Correct Answer: B

The number of inner loop iterations depends on the outer loop's variable `x`. - When x = 1, the inner loop condition `y < 1` is false. It runs 0 times. - When x = 2, the inner loop runs 1 time (y=1). - When x = 3, the inner loop runs 2 times (y=1, 2). - When x = 4, the inner loop runs 3 times (y=1, 2, 3). The total number of times `total++` is executed is 0 + 1 + 2 + 3 = 6.

Consider the following code segment. int check = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == j) { check++; } } } What is the final value of `check` after this code segment executes?

A) 1

B) 5

C) 10

D) 25

Correct Answer: B

The `check++` statement is executed only when the outer loop variable `i` is equal to the inner loop variable `j`. This condition is met 5 times: when (i, j) is (0, 0), (1, 1), (2, 2), (3, 3), and (4, 4). For all other 20 combinations, the condition is false.

Which of the following code segments will produce this output? 1 22 333 4444

A) for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { System.out.print(j); } System.out.println(); }

B) for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); }

C) for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { System.out.print(i); } System.out.println(); }

D) for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); }

Correct Answer: D

The outer loop controls the line number and the digit to be printed (1, 2, 3, 4). The inner loop controls how many times that digit is printed on the current line. The output shows that on line `i`, the digit `i` is printed `i` times. Option D correctly implements this logic by having the inner loop run from 1 to `i` and printing the value of `i` in each iteration.

Consider the following code segment. int result = 1; int i = 3; while (i > 0) { for (int j = 0; j < 2; j++) { result = result * 2; } i--; } What is the final value of `result`?

A) 8

B) 16

C) 32

D) 64

Correct Answer: D

The outer `while` loop runs 3 times (for i=3, 2, 1). For each of these 3 iterations, the inner `for` loop runs 2 times. This means the statement `result = result * 2;` is executed a total of 3 * 2 = 6 times. Starting with `result = 1`, multiplying by 2 six times gives 1 * 2 * 2 * 2 * 2 * 2 * 2 = 64.