## Section9.10Assessment: Arrays 1

### Subsection9.10.1Subgoals for Evaluating Arrays

1. Set up array from 0 to size-1

2. Evaluate data type of statements against array

3. Trace statements, updating slots as you go

1. Remember assignment subgoals

### Exercises9.10.2Exercises

#### 1.

Q1: Assuming that the following declaration has been made, which of the following code segments correctly interchanges the value of arr[0] and arr[5]?

int [] arr = new int[10];

• See diagram for answer A

• See diagram for answer B

• See diagram for answer C

• See diagram for answer D

• See diagram for answer E

#### 2.

Q2: Consider the following code that is intended to print true if all the elements in array arr are even numbers; otherwise it should print false. You may assume that arr has been declared and contains valid integer values.

boolean isEven = /* expression */ ;
for (int k = 0; k < arr.length; k++) {
/* loop body */
}
if (isEven)
System.out.println("TRUE");
else
System.out.println("FALSE");


Which of the following replacements for /* expression */ and /* loop body */ should be used so that the code works as intended?

• See diagram for answer A

• See diagram for answer B

• See diagram for answer C

• See diagram for answer D

• See diagram for answer E

#### 3.

Q3: Considering the following code, what are the values in numbers after execution?

int [] numbers = {17, 34, 21, 42, 15, 69, 48, 25, 39};
int x = 3;
for (int k = 1; k < numbers.length; k = k + x)
numbers[k] = numbers[k-1] + x;

• {17, 20, 21, 42, 45, 69, 48, 51, 39}

• {17, 20, 23, 26, 29, 32, 35, 38, 41}

• {17, 37, 21, 42, 18, 69, 48, 28, 39}

• {20, 23, 21, 42, 45, 69, 51, 54, 39}

• {20, 34, 21, 45, 15, 69, 51, 25, 39}