Skip to content
ExamSpine

Trace it

Trace: p2 b134 cinema free seats

Java code12 rows to fill4 marks

Complete the trace table for this Java code. The Lumen cinema stores one row of seats as an array: 0 is a free seat and 1 is a booked seat. This code goes along the row from seat 0. A cell stays empty for any variable a line leaves unchanged. Then answer the question below the table. [4]

Line 1: int[] row = {1, 0, 0, 1, 0};
Line 2: int free = 0;
Line 3: int first = -1;
Line 4: for (int i = 0; i < row.length; i++) {
Line 5, indented 1 level: if (row[i] == 0) {
Line 6, indented 2 levels: free = free + 1;
Line 7, indented 2 levels: if (first == -1) {
Line 8, indented 3 levels: first = i;
Line 9, indented 2 levels: }
Line 10, indented 1 level: }
Line 11: }

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

Rowifreefirst
1
2
3
4
5
6
7
8
9
10
11
12