Chapter 8: Loops
Multiple Choice · The for Loop
Question 1: How Many Times Is the Condition Checked?
In the loop below, how many times do the body (sum += i) and the condition (i <= n) run?
int n = 4; int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } System.out.println(sum);
B: The condition is true for i = 1, 2, 3, 4 and false for i = 5; that last (false) check is also an evaluation. The body runs 4 times and the condition is checked 5 times; the initialization (int i = 1) only once. Output 10.
Step-by-Step Trace · The for Loop
Question 2: The Value of i After continue
i is declared before the loop. What is i when the loop ends?
i is declared outside the loop: it lives on after the loop and can be printed.
Initialization: i = 1 (the old value 0 is overwritten). 1 < 6 → true; 1 > 3 false.
i++ → 2; 2 > 3 false. There is nothing else in the body.
i = 3; 3 > 3 false.
i = 4; 4 > 3 → continue: the rest of the round is skipped and it goes to the update (i++).
i = 5; continue again → i++.
i = 6; 6 < 6 → false: the loop ends. continue does not end the loop; break would.
6 is printed. A loop always ends at the first value that makes the condition false.
int i = 0; for (i = 1; i < 6; i++) { if (i > 3) continue; } System.out.println(i);
Multiple Choice · The for Loop
Question 3: i++ or ++i?
Which statement about the outputs of the two loops is true?
for (int i = 0; i < 3; i++) System.out.print(i + " "); System.out.println(); for (int i = 0; i < 3; ++i) System.out.print(i + " ");
B: In the update part of a for loop, the value of the expression is not used; both i++ and ++i just increase i by 1. The difference appears only when the value is used in an expression (e.g. b = a++).
Output Question · The for Loop
Question 4: result *= counter
What is the output when the method below is run?
void demo() { int result = 1; int counter = 6; for (int i = 3; i < counter; i++) { result *= counter; System.out.print(result + " "); } }
Show answer
Output: 6 36 216
- The trap: the multiplier is counter, not i; counter stays 6 the whole time. i only counts the rounds.
- i = 3, 4, 5 → 6 − 3 = 3 rounds: 6¹, 6², 6³.
Other starting values (result, counter, i): (1, 7, 4) → 7 49 343; (2, 7, 4) → 14 98 686; (2, 6, 3) → 12 72 432.
Output Question · while and do-while
Question 5: do-while Runs At Least Once
In both loops the condition is false from the start. What is the output?
int n = 5; do { System.out.print(n + " "); n++; } while (n < 3); int m = 5; while (m < 3) { System.out.print(m + " "); m++; } System.out.println("| " + n + " " + m);
Show answer
Output: 5 | 6 5
do-whilechecks the condition after the body: the body runs once (5 is printed, n = 6), then 6 < 3 is false.whilechecks the condition first: 5 < 3 is false → the body never runs, m stays 5.- do-while is chosen when “at least once” is needed, as in menus and input validation.
Multiple Choice · while and do-while
Question 6: Which Is an Infinite Loop?
Which of the code fragments below never ends?
C: Since there are no braces, the loop body is only print(i); i++ is a line that would run after the loop and is never reached. i stays 0 forever and 0 is printed endlessly. B: ends with 10 → 7 → 4 → 1 → −2.
Output Question · while and do-while
Question 7: A double Counter
The loop is expected to run 10 rounds. What is the output?
int n = 0; for (double x = 0; x < 1.0; x += 0.1) { n++; } System.out.println(n);
Show answer
Output: 11
- 0.1 cannot be represented exactly in binary; small rounding errors accumulate.
- After 10 additions x = 0.9999999999999999, not exactly 1.0 → the condition is true and the loop runs one round too many.
- The loop ends when x = 1.0999999999999999.
Lesson: use an int counter (e.g. for (int k = 0; k < 10; k++) and x = k * 0.1). Had x != 1.0 been written, the loop would never end.
Output Question · break and continue
Question 8: break and continue Together
What is the output of the code below?
for (int i = 1; i <= 12; i++) { if (i % 4 == 0) continue; if (i == 10) break; System.out.print(i + " "); } System.out.println("| end");
Show answer
Output: 1 2 3 5 6 7 9 | end
- 4 and 8:
continue→ printing is skipped, it goes on with i++. - 10:
break→ the loop ends at once; 11 and 12 are never tried. - Order matters: if the condition were
i == 12, 12 would first be skipped bycontinueandbreakwould never be reached.
Step-by-Step Trace · break and continue
Question 9: continue (Outer), break (Inner)
continue on i == 3 (outer), break on j == 3 (inner). Trace the output.
i = 1, j = 1 → print.
j = 2 → print.
j == 3 → break: only the inner loop ends.
i = 2: inner loop restarts at j = 1.
j = 2 → print.
j == 3 → break.
i == 3 → continue: row skipped.
i = 4, j = 1 → print.
j = 2 → print.
j == 3 → break.
Done: 6 lines printed.
for (int i = 1; i <= 4; i++) { if (i == 3) { continue; } for (int j = 1; j <= 4; j++) { if (j == 3) { break; } System.out.println("i: " + i + " j: " + j); } }
Multiple Choice · break and continue
Question 10: continue if j == 4
How many lines does the code print, and which is the last line?
for (int i = 2; i < 5; i++) { if (i == 4) { break; } for (int j = 2; j < 5; j++) { if (j == 4) { continue; } System.out.println("i: " + i + " j: " + j); } }
C: for i = 2 and 3, j = 2, 3 are printed and j = 4 is skipped: i: 2 j: 2, i: 2 j: 3, i: 3 j: 2, i: 3 j: 3. At i = 4 the outer break ends the loop, so there can be no i: 4 … line.
Output Question · break and continue
Question 11: Labeled break
What is the output with the labeled break outer? If the label were removed and a plain break written, how many number lines would appear?
outer: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i * j == 4) break outer; System.out.println(i + " " + j); } } System.out.println("done");
Show answer
1 1 1 2 1 3 2 1 done
- i = 2, j = 2: 2·2 = 4 →
break outerexits both loops at once; i = 3 is never tried. - A plain
breakends only the inner loop: for i = 3, 3 1, 3 2, 3 3 are also printed → 7 number lines + done.
Multiple Choice · Nested Loops
Question 12: How Many Times Does the Body Run?
The start of the inner loop depends on the outer variable. What is counter?
int counter = 0; for (int i = 1; i <= 4; i++) { for (int j = i; j <= 4; j++) { counter++; } } System.out.println(counter);
A: i = 1 → j = 1..4 (4 rounds), i = 2 → 3, i = 3 → 2, i = 4 → 1: 4 + 3 + 2 + 1 = 10. 16 would be if both loops ran 1..4 in full (4 × 4); here the inner loop is shorter each time.
Output Question · patterns
Question 13: Star and Dash Pattern
What is the output when the method below is run?
public static void draw() { for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); } for (int j = 0; j < 5 - i; j++) { System.out.print("-"); } System.out.println(); } }
Show answer
- Row i:
j <= i→ i + 1 stars (one more because it starts at 0). j < 5 − i→ 5 − i dashes.- Each row has 6 characters; the last row is
*****-(one dash remains).
Step-by-Step Trace · patterns
Question 14: Pyramid
In row i, rowCount − i spaces are printed first, then 2i − 1 stars. Build the output row by row.
Each row has 2 parts: spaces (inner loop j) and stars (inner loop k).
i = 1: 5 − 1 = 4 spaces, 2·1 − 1 = 1 star.
i = 2: 3 spaces, 3 stars.
i = 3: 2 spaces, 5 stars.
i = 4: 1 space, 7 stars.
i = 5: the j loop never runs (1 <= 0 is false), 9 stars.
6 <= 5 is false: done. The spaces shrink while the stars grow by 2; in total 1 + 3 + 5 + 7 + 9 = 25 stars.
int rowCount = 5; for (int i = 1; i <= rowCount; i++) { for (int j = 1; j <= rowCount - i; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print("*"); } System.out.println(); }
Multiple Choice · patterns
Question 15: Total Stars in the Pyramid
If the pyramid code is run with rowCount = 6, how many stars are in the last row and how many stars are printed in total?
A: Row i has 2i − 1 stars: i = 6 → 11. Total 1 + 3 + 5 + 7 + 9 + 11 = 36 = 6² (the sum of the first n odd numbers is n²). 21 = 1 + 2 + … + 6 is the total of an increasing triangle.
Output Question · patterns
Question 16: println in the Wrong Place
In the increasing-triangle code, println was accidentally put in the inner loop. What is the output, and how many lines are there?
int n = 3; for (int row = 1; row <= n; row++) { for (int column = 1; column <= row; column++) { System.out.print("*"); System.out.println(); } }
Show answer
Output: 6 lines one under another, a single * on each line:
* * * * * *
- println runs after every star: 1 + 2 + 3 = 6 stars → 6 lines.
- The correct way is to put println after the closing brace of the inner loop, inside the outer loop:
*,**,***.
Multiple Choice · Loop Examples
Question 17: Printing Even Numbers
Which fragment prints the even numbers from 1 to 100 (2, 4, …, 100)?
C: It starts at 2 and increases by 2, with 100 included (<=). A prints the odd numbers. B prints 0..98: 0 too many, 100 missing. In D, / is division, not remainder: only for i = 1 is 1 / 2 == 0 → prints 1. A solution written with if (i % 2 == 0) is also correct.
Step-by-Step Trace · Loop Examples
Question 18: Digit Count with do-while
digitCount(-305) is called. How many rounds does it loop?
Parameter number = −305.
Math.abs drops the sign: the digit count does not depend on the sign.
The counter is 0.
do: the body runs without checking the condition. Counter 1, 305 / 10 = 30 (the last digit is dropped).
30 > 0 → true. Counter 2, 30 / 10 = 3. The 0 in the middle is a digit too.
3 > 0 → true. Counter 3, 3 / 10 = 0.
0 > 0 → false: the loop ends.
3 rounds. If it were called with number = 0, the do-while would still run the body once → 1 (correct).
public static int digitCount(int number) { number = Math.abs(number); int digitCount = 0; do { digitCount++; number /= 10; } while (number > 0); return digitCount; }
Multiple Choice · Loop Examples
Question 19: The Digit Count of Zero
Three different digitCount solutions are given. Which one returns the wrong result (0) for digitCount(0)?
C: Since 0 > 0 is false, the while body never runs and 0 is returned; yet 0 has one digit. This solution also returns 0 for negative numbers (no Math.abs). A (do-while) and B (special case) are correct: both give 1.
Multiple Choice · Loop Examples
Question 20: 13! and int Overflow
The code tries to compute 13! = 6,227,020,800. Which statement is true?
int result = 1; for (int i = 1; i <= 13; i++) { result *= i; } System.out.println(result);
D: int holds at most 2,147,483,647; 13! exceeds this and the value silently wraps (no error) → 1932053504. 12! = 479001600 still fits. The fix is long result = 1; (it fits up to 20!). This is why a factorial method returns long.