Lesson 5.8 · Trace it
Trace: running total (Java)
Java code11 rows to fill3 marks
Complete the trace table for this Java code. Then answer the question below the table. [3]
Line 1: int[] values = {3, 5, 2, 7, 4};Line 2: int total = 0;Line 3: int i = 0;Line 4: while (total <= 10) {Line 5, indented 1 level: total = total + values[i];Line 6, indented 1 level: i = i + 1;Line 7: }Line 8: System.out.println(total);
Fill in one row each time a line changes total or i, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Java shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.
| Row | total | i | output |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 |