Skip to content
ExamSpine

Lesson 5.1 · Trace it

Trace: char at (Java)

Java code5 rows to fill4 marks

Complete the trace table for this Java code. word.charAt(i) is the character at index i, counting from 0, and word.length() is the number of characters. [4]

Line 1: String word = "code";
Line 2: int i = 1;
Line 3: char a = word.charAt(i);
Line 4: i = word.length() / 2;
Line 5: char b = word.charAt(i);
Line 6: System.out.println("" + a + b);

Fill in one row each time a line changes i, a or b, 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.

Rowiaboutput
1
2
3
4
5