Skip to content
ExamSpine

Lesson 5.11 · Trace it

Trace: carry larger (Java)

Java code9 rows to fill3 marks

Complete the trace table for this Java code. The loop starts i at 1, so it compares each value of the array with the one before it. Write the array in the nums column in square brackets, like [5, 2, 6, 1, 4], not in the curly brackets of the first line. Then answer the question below the table. [3]

Line 1: int[] nums = {5, 2, 6, 1, 4};
Line 2: for (int i = 1; i < nums.length; i++) {
Line 3, indented 1 level: if (nums[i] < nums[i - 1]) {
Line 4, indented 2 levels: nums[i] = nums[i - 1];
Line 5, indented 1 level: }
Line 6: }

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

Rowinums
1
2
3
4
5
6
7
8
9