← Unit 4: Looping
ICS3U · Unit 4 test review
Looping: Test Review
Everything for the Unit 4 test on one page: the key facts from all 3 lessons, the mistakes that cost the most marks, and a practice test that points you back to the lessons you need.
Key facts
while and do-while
- Every loop needs three parts: initialize before, a condition to test, and an update inside the body.
- A while loop tests first, so its body can run zero times.
- A do-while loop tests last, so its body always runs at least once, and its condition is followed by a semicolon.
- An infinite loop means the condition never becomes false — the update is almost always missing or wrong.
- A sentinel loop needs a priming read before the loop and another read at the end of the body.
count++is shorthand forcount = count + 1, andtotal += valueis shorthand fortotal = total + value.
for Loops and Counting
for (init; condition; update)— the initialization runs once, then the cycle is test, body, update, test.for (int i = 0; i < n; i++)runs exactly times, with taking the values to .- Counting by 1 from to inclusive runs times.
- A variable declared in the for header exists only inside the loop — using it afterwards will not compile.
- Use
forwhen the number of passes is known in advance,whilewhen it is not. - The enhanced for loop
for (int x : arr)gives you each value but no index.
Accumulators, Counters and Nested Loops
- Declare and initialize an accumulator before the loop; change it inside. Declaring it inside resets it every pass.
- A sum starts at 0, a product starts at 1, and a counter starts at 0.
- A max or min tracker starts at the first element, and its loop then starts at index 1.
- When both bounds are fixed, total inner passes = outer passes inner passes.
- When the inner bound depends on the outer counter, add the pass counts instead of multiplying.
breakexits the loop immediately;continueskips to the next pass. Both affect only the innermost loop containing them.
Where marks get lost
- Leaving out the update, so the condition never becomes false — the single most common cause of an infinite loop. (while and do-while)
- Writing a semicolon straight after the condition:
while (i < 5);compiles, but the semicolon becomes the entire body, so the loop does nothing forever. (while and do-while) - Declaring the counter inside the loop body. The condition cannot see it, so the code fails to compile with cannot find symbol rather than looping forever - declare the counter before the loop. (while and do-while)
- Forgetting the priming read in a sentinel loop, so the condition tests a variable that has no value yet and the program will not compile. (while and do-while)
- Reading the next value at the top of the body instead of the bottom, which adds the sentinel itself to the total before the loop notices it. (while and do-while)
- Using
<=where<belongs when the counter starts at 0 — the loop runs one extra time, and on an array that is an ArrayIndexOutOfBoundsException. (for Loops and Counting) - Writing a semicolon after the for header:
for (int i = 0; i < 5; i++);— the loop runs five empty passes and the block below runs once. (for Loops and Counting) - Reversing a loop by changing only
i++toi--and leaving the condition alone, which gives either zero passes or an infinite loop. (for Loops and Counting) - Changing the counter inside the body as well as in the update, so it advances by two each pass and the trace no longer matches the header. (for Loops and Counting)
- Declaring
int iin the for header and then trying to useiafter the loop — the variable no longer exists there, so it will not compile. (for Loops and Counting) - Declaring the accumulator inside the loop, so it resets every pass and the final answer is just the last value processed. (Accumulators, Counters and Nested Loops)
- Starting a running product at 0 instead of 1 — zero multiplied by anything is zero, so the answer is always 0. (Accumulators, Counters and Nested Loops)
- Starting a max tracker at 0 when the data can be negative, which reports a maximum that is not in the data at all. (Accumulators, Counters and Nested Loops)
- Printing the total inside the loop instead of after it, which produces one line per pass instead of one final answer. (Accumulators, Counters and Nested Loops)
- Putting the row-ending
System.out.println();inside the inner loop, so every single item lands on its own line. (Accumulators, Counters and Nested Loops)
Practice test
18 questions from across the unit. Each one shows the lesson it comes from.
- while and do-while1A loop prints the same value forever. Which part is most likely missing?
- while and do-while2In that same loop, what is the value of
iimmediately after the loop ends? - while and do-while3Trace and give the final value of
steps:int n = 20; int steps = 0; while (n > 1) { n = n / 2; steps++; } - while and do-while4Why does
while (count < 5);followed by an indented block of statements fail? - while and do-while5Put the parts of a correct sentinel-controlled input loop in order.
- 1Prompt and read the next value at the end of the body
- 2Update the accumulator with the current value
- 3Report the result after the loop finishes
- 4Initialize the accumulator before the loop
- 5Prompt and read the first value (the priming read)
- 6Test the value against the sentinel in the loop condition
- while and do-while6A program reads prices until the user types -1, but the reported total is 1 lower than expected. What is the likely cause?
- for Loops and Counting7How many times does the body of
for (int i = 0; i < 8; i++)run? - for Loops and Counting8How many times does the body of
for (int i = 5; i <= 50; i += 5)run? - for Loops and Counting9What is printed first by
for (int i = 0; i < 3; i++) { System.out.println(i); }? - for Loops and Counting10Why can an enhanced for loop
for (int mark : marks)never throw an ArrayIndexOutOfBoundsException? - for Loops and Counting11Match each header to the number of passes it produces.for (int i = 0; i < 6; i++)for (int i = 1; i <= 6; i++)for (int i = 0; i <= 6; i++)for (int i = 1; i < 6; i++)
- for Loops and Counting12A loop over an array of 6 marks crashes with ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6. What is the fix?
- Accumulators, Counters and Nested Loops13What is the final value of
total?int[] m = {78, 85, 92, 64}; int total = 0; for (int i = 0; i < m.length; i++) { total += m[i]; } - Accumulators, Counters and Nested Loops14What is the final value of
product?int product = 1; for (int i = 1; i <= 5; i++) { product = product * i; } - Accumulators, Counters and Nested Loops15How many times does the inner body run?
for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { System.out.print(r * c); } } - Accumulators, Counters and Nested Loops16What is printed?
int[] v = {4, 9, -1, 7, 12}; int t = 0; for (int i = 0; i < v.length; i++) { if (v[i] < 0) { continue; } t += v[i]; } System.out.println(t); - Accumulators, Counters and Nested Loops17Order the steps of writing a loop that finds the average of an array.
- 1Print the average
- 2After the loop, cast the total to double and divide by the length
- 3Add the current element to the accumulator inside the body
- 4Declare and initialize the accumulator to 0, before the loop
- 5Write a for loop that visits every index from 0 to length - 1
- Accumulators, Counters and Nested Loops18A nested loop prints every item on its own line instead of in rows. What is wrong?
Endless drill
Fresh numbers every time, for the calculation questions in this unit.
Endless drillnew numbers every time
Generating a problem…