Lesson 5.4 · Trace it
Trace: Java reverse
Java code9 rows to fill3 marks
Complete the trace table for this Java code. word.charAt(i) is the character at index i, counting from 0. i-- takes 1 away from i, the opposite of i++, so this loop counts down. [3]
Line 1: String word = "cab";Line 2: String result = "";Line 3: for (int i = word.length() - 1; i >= 0; i--) {Line 4, indented 1 level: result = result + word.charAt(i);Line 5: }Line 6: System.out.println(result);
Fill in one row each time a line changes i or result, 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 | i | result | output |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 |