Java Topic Practice
For Loops
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
For practice use only.
For Loops MCQ Practice
For Loops Java practice with 15 questions, code tracing, and a worked explanation for every answer.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - For Loops: track the final printed value.
int total = 0;
for (int i = 1; i <= 5; i += 2) {
total += i + 2;
}
System.out.print(total);All 15 for loops questions
Work through the interactive quiz above first. This is the full for loops question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - For Loops: track the final printed value.
int total = 0; for (int i = 1; i <= 5; i += 2) { total += i + 2; } System.out.print(total);- 16
- 7
- 15
- 13
Show worked solution
Correct answer: 15
The for loop visits odd i values up to the limit and adds i plus the constant each time.
2.Code Tracing 12 - For Loops: follow the variable updates.
int value = 26; int count = 0; while (value > 7) { value -= 5; count++; } System.out.print(count + ":" + value);- 3:6
- 4:11
- 5:1
- 4:6
Show worked solution
Correct answer: 4:6
The while loop keeps subtracting until value is no longer greater than the limit.
3.Code Tracing 28 - For Loops: trace the branch and loop path.
int total = 0; for (int outer = 1; outer <= 3; outer++) { for (int inner = 0; inner < outer + 2; inner++) { total += outer + inner; } } System.out.print(total);- 42
- 48
- 50
- 45
Show worked solution
Correct answer: 45
For each outer value, the inner loop runs its full range before outer advances.
4.Code Tracing 39 - For Loops: watch the index changes.
int count = 0; for (int value = 5; value <= 46; value += 5) { if (value % 2 == 0) { count++; } } System.out.print(count);- 4
- 5
- 3
- 9
Show worked solution
Correct answer: 4
The counter only increases on loop values that are even.
5.Code Tracing 50 - For Loops: evaluate the state change step by step.
int[] data = {13, 9, 6, 8, 8, 10}; int total = 0; int i = 0; while (i < data.length) { total += data[i] % 7; i += 2; } System.out.print(total);- 6
- 13
- 15
- 7
Show worked solution
Correct answer: 13
The loop visits indexes 0, 2, and 4, adding each selected value modulo the divisor.
6.What is printed by the following code segment?
int t = 0; for (int i = 1; i <= 4; i++) { for (int j = i; j <= 4; j++) { t += j - i; } } System.out.print(t);- 16
- 10
- 6
- 20
Show worked solution
Correct answer: 10
The inner sum is 6 when i is 1, 3 when i is 2, 1 when i is 3, and 0 when i is 4.
7.What is printed by the following code segment?
int t = 0; for (int i = 10; i > 0; i -= 3) { t = t * 2 + i % 3; } System.out.print(t);- 10
- 7
- 15
- 8
Show worked solution
Correct answer: 15
The values 10, 7, 4, and 1 each leave remainder 1, and doubling the accumulator each pass builds 1, 3, 7, then 15.
8.What is printed by the following code segment?
String s = ""; for (int i = 1; i <= 4; i++) { s = i % 2 == 0 ? s + i : i + s; } System.out.print(s);- 1234
- 4321
- 1324
- 3124
Show worked solution
Correct answer: 3124
Odd values are prepended and even values appended, so the string grows 1, then 12, then 312, then 3124.
9.What is printed by the following code segment?
int c = 0; for (int i = 0; i < 20; i++) { if (i % 4 == 0) { continue; } if (i % 7 == 0) { break; } c++; } System.out.print(c);- 5
- 6
- 14
- 15
Show worked solution
Correct answer: 5
Multiples of 4 are skipped and the loop ends at 7, so 1, 2, 3, 5, and 6 are counted.
10.What is printed by the following code segment?
int[] a = {2, 4, 6, 8}; int t = 0; for (int i = 0; i < a.length; i += 2) { for (int j = 1; j < a.length; j += 2) { t += a[j] - a[i]; } } System.out.print(t);- 12
- 8
- 16
- 4
Show worked solution
Correct answer: 8
The outer index takes 0 and 2 while the inner takes 1 and 3, and the four differences are 2, 6, -2, and 2.
11.What is printed by the following code segment?
int n = 1; for (int i = 1; i <= 5; i++) { n *= i % 3 == 0 ? 2 : 1; } System.out.print(n);- 1
- 4
- 2
- 8
Show worked solution
Correct answer: 2
Only i equal to 3 is a multiple of 3 within the range, so the product doubles exactly once.
12.What is printed by the following code segment?
int t = 0; for (int i = 1; i < 6; i++) { t += i % 2 == 0 ? -i : i; } System.out.print(t);- -3
- 15
- 9
- 3
Show worked solution
Correct answer: 3
The signs alternate, giving 1 - 2 + 3 - 4 + 5.
13.What is printed by the following code segment?
int count = 0; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { if (i * j % 2 == 1) { count++; } } } System.out.print(count);- 4
- 8
- 6
- 12
Show worked solution
Correct answer: 4
A product is odd only when both factors are odd, and there are two odd values in the range.
14.What is printed by the following code segment?
int[] a = new int[5]; for (int i = 0; i < a.length; i++) { a[i] = i == 0 ? 1 : a[i - 1] * 2; } System.out.print(a[4]);- 8
- 16
- 32
- 10
Show worked solution
Correct answer: 16
Each element doubles the one before it, so the array holds 1, 2, 4, 8, and 16.
15.What is printed by the following code segment?
int s = 0; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == i) { continue; } s += i * 10 + j; } } System.out.print(s);- 198
- 120
- 132
- 144
Show worked solution
Correct answer: 132
The diagonal pairs are skipped, leaving six two-digit combinations.
