Skip to content
ExamSpine

Lesson 5.7 · Trace it

Trace: double loop (Java)

Java code9 rows to fill4 marks

Complete the trace table for this Java code. for (int x : nums) gives x each value of nums in turn. size is the number of values. [4]

Line 1: int[] nums = {4, 1, 3};
Line 2: for (int x : nums) {
Line 3, indented 1 level: x = x * 10;
Line 4: }
Line 5: int first = nums[0];
Line 6: int size = nums.length;
Line 7: for (int i = 0; i < size; i++) {
Line 8, indented 1 level: nums[i] = nums[i] + 1;
Line 9: }
Line 10: int last = nums[2];
Line 11: System.out.println(first + last);

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

Rowxfirstlastoutput
1
2
3
4
5
6
7
8
9