9.4. Assessment: Arrays 1ΒΆ
Subgoals for Evaluating Arrays
Set up array from 0 to size-1
Evaluate data type of statements against array
Trace statements, updating slots as you go
Remember assignment subgoals
- 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
- 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
- {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}
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];

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?

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;