Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1for (let i = 1; i <= 3; i++) { console.log(i); } | AThe total was not added up. It should keep a running sum: 1, then 3, then 6, because each repeat adds i to the previous total. | |
| 2let total = 0; let n = 1; while (n <= 3) { total += n; n++; } | Bfor (let i = 2; i <= 6; i += 2) { console.log(i); } // Prediction: 2, 4, 6 | |
| 3Predicting the output by hand: before running, fill in a trace table, then write down what the loop will print. | CTrace table columns: n and total. Row 1: n=1, total=1. Row 2: n=2, total=3. Row 3: n=3, total=6. Then n is 4 and the loop stops. | |
| 4Comparing your trace with the real output: if the console output differs from your table, you made a tracing mistake or the code has a bug. | DThe trace includes a row for i = 4, but the condition i < 4 is false when i is 4, so the loop body never runs for it. Only 3 rows are needed. | |
| 5// Trace of: for (let i = 1; i < 4; i++) // Rows: i=1, i=2, i=3, i=4 | Elet count = 3; while (count > 0) { console.log(count); count--; } // Trace: 3, 2, 1 | |
| 6// Trace of: let total = 0; // for (let i = 1; i <= 3; i++) { total += i; } // Rows: total=1, total=2, total=3 | FA trace table for this loop has 3 rows: i is 1, then 2, then 3, with output 1, 2, 3. It stops when i becomes 4. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
trace table — A table with one row per loop repeat, showing the counter, other variables and any output, filled in by hand until the condition is false.for (let i = 1; i <= 3; i++) {
console.log(i);
}for (let i = 1; i <= 3; i++) {
console.log(i);
}let total = 0;
let n = 1;
while (n <= 3) { total += n; n++; }let total = 0;
for (let i = 1; i <= 4; i++) { total += i; }
console.log(total);let n = 3;
while (n > 0) { console.log(n); n--; }
// trace rows: n = 3, 2, 1