Iteration Practice Questions

AP (Advanced Placement) · AP Computer Science A · 149 free MCQs with instant results and detailed explanations.

149
Total
51
Easy
72
Medium
26
Hard

Start Practicing Iteration

Take a timed quiz or customize your practice session

Quick Quiz (10 Qs) → Mock Test (25 Qs) ⚙ Customize

Sample Questions from Iteration

Here are 10 sample questions. Start a quiz to get randomized questions with scoring.

Q1
Easy
What is the output of the following code snippet? ```java int sum = 0; for (int i = 1; i <= 5; i++) { sum += i; } System.out.println(sum); ```
A. 15
B. 10
C. 5
D. 20
Show Answer & Explanation
Correct Answer: A
The loop iterates from 1 to 5, adding each value to 'sum'. The sum of 1 + 2 + 3 + 4 + 5 equals 15.
Q2
Easy
Which of the following for-loops correctly prints all even numbers from 2 to 10 inclusive?
A. for (int i = 2; i <= 10; i += 2) { System.out.println(i); }
B. for (int i = 1; i < 10; i++) { if (i % 2 == 0) System.out.println(i); }
C. for (int i = 2; i < 10; i += 2) { System.out.println(i); }
D. for (int i = 0; i <= 10; i += 2) { System.out.println(i); }
Show Answer & Explanation
Correct Answer: A
Option A increments by 2 starting from 2, printing 2, 4, 6, 8, and 10. It correctly prints all even numbers up to 10.
Q3
Easy
What will be the final value of 'count' after the following while loop executes? ```java int count = 0; int n = 5; while (n > 0) { count++; n--; } ```
A. 5
B. 0
C. 10
D. 1
Show Answer & Explanation
Correct Answer: A
The loop counts down from n = 5 to 0, incrementing 'count' each time. Therefore, 'count' will be 5 after 5 iterations.
Q4
Medium
What will be the output of the following code snippet? ```java int sum = 0; for (int i = 1; i <= 5; i++) { sum += i; } System.out.println(sum); ```
A. 10
B. 15
C. 20
D. 5
Show Answer & Explanation
Correct Answer: B
The code calculates the sum of integers from 1 to 5. The formula for the sum of the first n integers is n(n+1)/2. Here, n is 5, so the sum is 5(5+1)/2 = 15.
Q5
Medium
Consider the following method: ```java public void countDown(int n) { while (n > 0) { System.out.print(n + " "); n--; } } ``` What will be printed when `countDown(3)` is executed?
A. 3 2 1
B. 3 2 1 0
C. 2 1 0
D. 3 2 1 0 1
Show Answer & Explanation
Correct Answer: A
The method starts with n = 3 and prints n while decreasing it until n is no longer greater than 0. Hence, the output will be 3 2 1.
Q6
Medium
What is the output of the following code? ```java for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { System.out.print(i + j + " "); } } ```
A. 123
B. 123 124 134
C. 23 24 34
D. 3 4 5
Show Answer & Explanation
Correct Answer: B
The outer loop iterates i from 1 to 3, while the inner loop iterates j from 1 to 2. The output will be: 1+1, 1+2, 2+1, 2+2, 3+1, 3+2, resulting in 2 3 3 4 4 5.
Q7
Medium
What will be the value of `total` after executing the following code? ```java int total = 0; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { total++; } } ```
A. 4
B. 10
C. 16
D. 20
Show Answer & Explanation
Correct Answer: B
The inner loop runs j from 1 to i, so for i=1 it runs 1 time, for i=2 it runs 2 times, for i=3 it runs 3 times, and for i=4 it runs 4 times. Total increments: 1+2+3+4 = 10.
Q8
Hard
What will be the output of the following code snippet? ```java int sum = 0; for (int i = 1; i <= 5; i++) { if (i % 2 == 0) { continue; } sum += i; } System.out.println(sum); ```
A. 6
B. 9
C. 10
D. 15
Show Answer & Explanation
Correct Answer: B
The loop iterates from 1 to 5. The `continue` statement skips addition for even numbers (2 and 4). The odd numbers 1, 3, and 5 are added up: 1 + 3 + 5 = 9.
Q9
Hard
Consider the following code segment: ```java int total = 1; int n = 6; for (int i = 1; i <= n; i++) { total *= i; } System.out.println(total); ``` What does this code compute?
A. 6
B. 720
C. 36
D. 120
Show Answer & Explanation
Correct Answer: B
The code computes the factorial of n (6). The loop multiplies `total` by each integer from 1 to 6 (1 * 2 * 3 * 4 * 5 * 6 = 720).
Q10
Hard
Consider the following code snippet in Java: ```java int sum = 0; for (int i = 1; i <= 5; i++) { sum += i; } System.out.println(sum); ``` What will be the output of this code?
A. 10
B. 15
C. 20
D. 25
Show Answer & Explanation
Correct Answer: B
The code adds the integers from 1 to 5 inclusive. The sum is calculated as: 1 + 2 + 3 + 4 + 5 = 15.

Showing 10 of 149 questions. Start a quiz to practice all questions with scoring and timer.

Practice All 149 Questions →

Iteration โ€” AP (Advanced Placement) AP Computer Science A Practice Questions Online

This page contains 149 practice MCQs for the chapter Iteration in AP (Advanced Placement) AP Computer Science A. The questions are organized by difficulty โ€” 51 easy, 72 medium, 26 hard โ€” so you can choose the right level for your preparation.

Every question includes a detailed explanation to help you understand the concept, not just memorize answers. Take a timed quiz to simulate exam conditions, or practice at your own pace with no time limit.