Lesson 6.2 · Trace it
Trace: find skipped value (Java)
Java code8 rows to fill4 marks
Complete the trace table for this Java code. This code is meant to add up every value in readings. The right total is 21. The code runs with no error message, but total ends up wrong. Then answer the question below the table. [4]
Line 1: int[] readings = {6, 2, 9, 4};Line 2: int total = 0;Line 3: int i = 1;Line 4: while (i < readings.length) {Line 5, indented 1 level: total = total + readings[i];Line 6, indented 1 level: i = i + 1;Line 7: }
Fill in one row each time a line changes i or total. 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.
| Row | i | total |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
| 7 | ||
| 8 |