# Multiple-Choice Exercises¶

## Easier Multiple Choice Questions¶

`nums.length`

- Since the first element in an array is at index 0 the last element is the length minus 1.
`nums.length - 1`

- Since the first element in an array is at index 0 the last element is the length minus 1.

7-7-1: Which index is the last element in an array called `nums`

at?

`int[] scores = null;`

- You can initialize an array reference to null to show that it doesn't refer to any array yet.
`int[] scoreArray = {50,90,85};`

- You can provide the values for an array when you declare it.
`String[] nameArray = new String[10];`

- You can declare and array and create the array using the
`new`

operator in the same statement. `String[] nameArray = {5, 3, 2};`

- You can not put integers into an array of String objects.
`int[] scores = new int[5];`

- You can declare and array and create it in the same statement. Use the
`new`

operator to create the array and specify the size in square brackets.

7-7-2: Which of the following declarations will cause a compile time error?

- 1
- This would be returned from
`arr[2]`

. - 2
- This returns the value in
`arr`

at index 3. Remember that the first item in an array is at index 0. - 3
- This would be returned from
`arr[1]`

. - 6
- This would be returned from
`arr[0]`

. - 4
- This would be returned from
`arr.length`

7-7-3: What is returned from `arr[3]`

if `arr={6, 3, 1, 2}`

?

- 17.5
- This would be true if the loop stopped at
`arr.length - 1`

. - 30.0
- This would be true if the loop started at 1 instead of 0.
- 130
- This would be true if it returned
`output`

rather than`output / arr.length`

- 32
- This would be true if
`output`

was declared to be an int rather than a double. - 32.5
- This sums all the values in the array and then returns the sum divided by the number of items in the array. This is the average.

7-7-4: What is returned from `mystery`

when it is passed `{10, 30, 30, 60}`

?

```
public static double mystery(int[] arr)
{
double output = 0;
for (int i = 0; i < arr.length; i++)
{
output = output + arr[i];
}
return output / arr.length;
}
```

You can step through the code above using the Java Visualizer by clicking on the following link Prob-7-9-4.

- {-20, -10, 2, 8, 16, 60}
- This would true if it looped through the whole array. Does it?
- {-20, -10, 2, 4, 8, 30}
- This would be true if it looped from the beginning to the middle. Does it?
- {-10, -5, 1, 8, 16, 60}
- It loops from the middle to the end doubling each value. Since there are 6 elements it will start at index 3.
- {-10, -5, 1, 4, 8, 30}
- This would be true if array elements didn't change, but they do.

7-7-5: Given the following values of `a`

and the method `doubleLast`

what will the values of `a`

be after you execute: `doubleLast()`

?

```
private int[ ] a = {-10, -5, 1, 4, 8, 30};
public void doubleLast()
{
for (int i = a.length / 2; i < a.length; i++)
{
a[i] = a[i] * 2;
}
}
```

You can step through the code above using the Java Visualizer by clicking on the following link Prob-7-9-5.

- {1, 3, -5, -2}
- This would be true if the contents of arrays could not be changed but they can.
- {3, 9, -15, -6}
- This code multiplies each value in a by the passed amt which is 3 in this case.
- {2, 6, -10, -4}
- This would be correct if we called multAll(2) instead of multAll(3).
- The code will never stop executing due to an infinite loop
- The variable i starts at 0 and increments each time through the loop and stops when it equals the number of items in a.

7-7-6: What are the values in a after multAll(3) executes?

```
private int[ ] a = {1, 3, -5, -2};
public void multAll(int amt)
{
int i = 0;
while (i < a.length)
{
a[i] = a[i] * amt;
i++;
} // end while
} // end method
```

- {1, 3, -5, -2}
- Does the value of i ever change inside the loop?
- {3, 9, -15, -6}
- Does the value of i ever change inside the loop?
- {2, 6, -10, -4}
- Does the value of i ever change inside the loop?
- The code will never stop executing due to an infinite loop
- The value of i is initialized to 0 and then never changes inside the body of the loop, so this loop will never stop. It is an infinite loop.

7-7-7: What are the values in a after mult(2) executes?

```
private int[ ] a = {1, 3, -5, -2};
public void mult(int amt)
{
int i = 0;
while (i < a.length)
{
a[i] = a[i] * amt;
} // end while
} // end method
```

- 2
- The size of outer array is the number of rows. Remember that two-dimensional arrays are actually an array of arrays in Java.
- 4
- The size of the inner array is the number of columns.
- 8
- This is the total number of items in the array.

7-7-8: How many columns does `a`

have if it is created as follows `int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};`

?

You can see how the array looks by clicking on the following Ex-9-7-1.

`strGrid[0][2] = "S";`

- The code
`letterGrid[0][2] = "S";`

actually sets the 1st row and 3rd column to hold a reference to the`String`

object "S". `strGrid[1][3] = "S";`

- This would be true if row and column indicies started at 1 instead of 0 and if this was in column major order.
`strGrid[3][1] = "S";`

- This would be true if row and column indicies started at 1 instead of 0.
`strGrid[2][0] = "S";`

- In row-major order the row is specified first followed by the column. Row and column indicies start with 0. So
`letterGrid[2][0]`

is the 3rd row and 1st column. `strGrid[0][0] = "S";`

- This would set the element at the first row and column.

7-7-9: Which of the following statements assigns the letter S to the third row and first column of a two-dimensional array named `strGrid`

(assuming row-major order).

- a[0][3]
- This would be true if the row index started at 0, but the column index started at 1.
- a[1][3]
- Both the row and column indicies start with 0.
- a[0][2]
- The value 6 is at row 0 and column 2.
- a[2][0]
- The row index is specified first, then the column index.
- a[3][1]
- The row index is specified first and the indicies start at 0.

7-7-10: How would you get the value 6 out of the following array `int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};`

?

## Medium Multiple Choice Questions¶

- The value in
`b[0]`

does not occur anywhere else in the array - The assertion denotes that
`b[0]`

occurs only once, regardless of the order or value of the other array values. - Array
`b`

is sorted - The array does not necessarily need to be in order for the assertion to be true.
- Array
`b`

is not sorted - We can't tell if it is sorted or not from this assertion.
- Array
`b`

contains no duplicates - The only value that must not have a duplicate is
`b[0]`

- The value in
`b[0]`

is the smallest value in the array `b[0]`

can be any value, so long as no other array element is equal to it.

7-7-11: Which of the following statements is a valid conclusion. Assume that variable `b`

is an array of `k`

integers and that the following is true:

```
b[0] != b[i] for all i from 1 to k-1
```

- whenever the first element in
`a`

is equal to`val`

- It is the last value in
`a`

that controls the final state of`temp`

, as the loop is progressing through the array from 0 to the end. - Whenever
`a`

contains any element which equals`val`

- Because
`temp`

is reset every time through the loop, only the last element controls whether the final value is true or false. - Whenever the last element in
`a`

is equal to`val`

- Because each time through the loop
`temp`

is reset, it will only be returned as true if the last value in`a`

is equal to`val`

. - Whenever more than 1 element in
`a`

is equal to`val`

- Because
`temp`

is reset every time through the loop, only the last element controls whether the final value is true or false, so it is possible for just the last value to be equal to`val`

. - Whenever exactly 1 element in
`a`

is equal to`val`

- Because
`temp`

is reset every time through the loop, only the last element controls whether the final value is true or false, so it is possible for several elements to be equal to`val`

.

7-7-12: Consider the following code segment. Which of the following statements best describes the condition when it returns true?

```
boolean temp = false;
for (int i = 0; i < a.length; i++) {
temp = (a[i] == val);
}
return temp;
```

You can step through the code above with the Java Visualizer by clicking the following link Prob-7-10-2.

- It is the length of the shortest consecutive block of the value
`target`

in`nums`

- It doesn't reset
`lenCount`

ever, so it just counts all the times the`target`

value appears in the array. - It is the length of the array
`nums`

- The only count happens when
`lenCount`

is incremented when`nums[k] == target`

.`nums.length`

is only used to stop the loop. - It is the length of the first consecutive block of the value
`target`

in`nums`

- It doesn't reset
`lenCount`

ever, so it just counts all the times the`target`

value appears in the array. - It is the number of occurrences of the value
`target`

in`nums`

- The variable
`lenCount`

is incremented each time the current array element is the same value as the`target`

. It is never reset so it counts the number of occurrences of the value`target`

in`nums`

. The method returns`maxLen`

which is set to`lenCount`

after the loop finishes if`lenCount`

is greater than`maxLen`

. - It is the length of the last consecutive block of the value
`target`

in`nums`

- It doesn't reset
`lenCount`

ever, so it just counts all the times the`target`

value appears in the array.

7-7-13: Consider the following data field and method `findLongest`

. Method `findLongest`

is intended to find the longest consecutive block of the value `target`

occurring in the array `nums`

; however, `findLongest`

does not work as intended. For example given the code below the call `findLongest(10)`

should return 3, the length of the longest consecutive block of 10s. Which of the following best describes the value actually returned by a call to `findLongest`

?

```
private int[] nums = {7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10};
public int findLongest(int target) {
int lenCount = 0; // length of current consecutive numbers
int maxLen = 0; // max length of consecutive numbers
for (int k = 0; k < nums.length; k++) {
if (nums[k] == target) {
lenCount++;
} else if (lenCount > maxLen) {
maxLen = lenCount;
}
}
if (lenCount > maxLen) {
maxLen = lenCount;
}
return maxLen;
}
```

You can step through the code above with the Java Visualizer by clicking the following link Prob-7-10-3. Can you fix the code in the Java Visualizer so that it works as intended?

- All values in positions
`m+1`

through`myStuff.length-1`

are greater than or equal to`n`

. - Mystery steps backwards through the array until the first value less than the passed
`num`

(`n`

) is found and then it returns the index where this value is found. Nothing is known about the elements of the array prior to the index at which the condition is met. - All values in position 0 through
`m`

are less than`n`

. - Mystery steps backwards through the array and quits the first time the value at the current index is less than the passed
`num`

(`n`

). This would be true if we went forward through the array and returned when it found a value greater than the passed`num`

(`n`

). - All values in position
`m+1`

through`myStuff.length-1`

are less than`n`

. - This would be true if it returned when it found a value at the current index that was greater than
`num`

(`n`

). - The smallest value is at position
`m`

. - The condition compares the value at the current index of the array to the passed
`num`

. It returns the first time the condition is met so nothing is known about the values which are unchecked. One of the unchecked values could be smaller. - The largest value that is smaller than
`n`

is at position`m`

. - The condition checks for any value that is smaller than the passed
`num`

and returns from`mystery`

the first time that the condition is encountered. The values are not ordered so we don't know if this is the largest value smaller than`n`

.

7-7-14: Consider the following data field and method. Which of the following best describes the contents of `myStuff`

in terms of `m`

and `n`

after the following statement has been executed?

```
private int[] myStuff;
//precondition: myStuff contains
// integers in no particular order
public int mystery(int num) {
for (int k = myStuff.length - 1; k >= 0; k--) {
if (myStuff[k] < num) {
return k;
}
}
return -1;
}
int m = mystery(n)
```

You can step through the code above with the Java Visualizer by clicking the following link Prob-7-10-4.

- Returns the index of the largest value in array
`arr`

. - This code sets
`loc`

to the middle of the array and then loops through all the array elements. If the value at the current index is greater than the value at`loc`

then it changes`loc`

to the current index. It returns`loc`

, which is the index of the largest value in the array. - Returns the index of the first element in array
`arr`

whose value is greater than`arr[loc]`

. - This would be true if there was a
`return loc`

after`loc = k`

in the`if`

block. - Returns the index of the last element in array
`arr`

whose value is greater than`arr[loc]`

. - This would be true if it returned
`loc`

after setting`loc = k`

and if it started at the end of the array and looped toward the beginning of the array. - Returns the largest value in array
`arr`

. - It returns the
*index*to the largest value in array`arr`

, not the largest value. - Returns the index of the largest value in the second half of array
`arr`

. `k`

loops from 0 to`arr.length - 1`

. So it checks all of the elements in the array.

7-7-15: Consider the following field `arr`

and method `checkArray`

. Which of the following best describes what `checkArray`

returns?

```
private int[] arr;
// precondition: arr.length != 0
public int checkArray()
{
int loc = arr.length / 2;
for (int k = 0; k < arr.length; k++)
{
if (arr[k] > arr[loc])
{
loc = k;
}
}
return loc;
}
```

You can step through the code above with the Java Visualizer by clicking the following link Prob-7-10-5.

- 4
- This would be true if it was
`return (a[1] *= 2);`

, which would change the value at`a[1]`

. - 2
- The statement
`a[1]--;`

is the same as`a[1] = a[1] - 1;`

so this will change the 3 to 2. The`return (a[1] * 2)`

does not change the value at`a[1]`

. - 12
- This would be true if array indicies started at 1 instead of 0 and if the code changed the value at index 1 to the current value times two.
- 6
- This would be true if array indices started at 1 rather than 0.
- 3
- This can't be true because
`a[1]--;`

means the same as`a[1] = a[1] - 1;`

so the 3 changes to 2. Parameters are all pass by value in Java which means that a copy of the value is passed to a method. But, since an array is an object a copy of the value is a copy of the reference to the object. So changes to objects in methods are permanent.

7-7-16: Given the following field and method declaration, what is the value in `a[1]`

when `m1(a)`

is run?

```
int[] a = {7, 3, -1};
public static int m1(int[] a)
{
a[1]--;
return (a[1] * 2);
}
```

You can step through the code above with the Java Visualizer by clicking the following link Prob-7-10-6.

- k - 1
- This loop will start at 1 and continue until
`k`

is reached as long as`arr[i] < someValue`

is true. The last time the loop executes,`i`

will equal`k-1`

, if the condition is always true. The number of times a loop executes is equal to the largest value when the loop executes minus the smallest value plus one. In this case that is`(k - 1) - 1 + 1`

which equals`k - 1`

. - k + 1
- This would be true if
`arr[i] < someValue`

was always true and the loop started at 0 instead of 1 and continued while it was less than or equal to`k`

. - k
- This would be true if
`arr[i] < someValue`

was always true and the loop started at 0 instead of 1. - 1
- This would be the case if only one element in the array would fulfill the condition that
`arr[i] < someValue`

. - 0
- This is the minimum number of times that
`HELLO`

could be executed. This would be true if`k`

was less than`i`

initially.

7-7-17: Consider the following code. What is the *maximum* amount of times that `HELLO`

could possibly be printed?

```
for (int i = 1; i < k; i++)
{
if (arr[i] < someValue)
{
System.out.print("HELLO")
}
}
```

You can step through the code above with the Java Visualizer by clicking the following link Prob-7-10-7.

- {2, 6, 2, -1, -3}
- This would be correct if
`data[k]`

was modified in the for-loop. In this for-loop,`data[k - 1]`

is the element that changes. - {-23, -21, -13, -3, 6}
- This would be correct if
`data[k - 1]`

was subtracted from`data[k]`

. Notice that for every instance of the for-loop,`data[k]`

and`data[k - 1]`

are added together and`data[k - 1]`

is set to that value. - {10, 18, 19, 15, 6}
- This would be correct if the for-loop began at 1 and continued to
`data.length - 1`

. Notice the for-loop indexing. - This method results in an IndexOutOfBounds exception.
- The indexing of this method is correct. The for-loop begins at the last valid index and ends when
`k`

is equal to 0, and the method does not access any values other than the ones specified. - {35, 33, 25, 15, 6}
- This method starts at the last valid index of the array and adds the value of the previous element to the element at index
`k - 1`

.

7-7-18: Consider the following method `changeArray`

. An array is created that contains `{2, 8, 10, 9, 6}`

and is passed to `changeArray`

. What are the contents of the array after the `changeArray`

method executes?

```
public static void changeArray(int[] data)
{
for (int k = data.length - 1; k > 0; k--)
data[k - 1] = data[k] + data[k - 1];
}
```

You can step through the code above with the Java Visualizer by clicking the following link Prob-7-10-8.

- [-2, -1, -5, 3, -4]
- This would be true if
`i`

started at 0 instead of`arr1.length / 2`

. - [-2, -1, 3, -8, 6]
- This would be true if
`i`

started at 0 and ended when it reached`arr1.length / 2`

. - [1, 5, -5, 3, -4]
- This loop starts at
`arr2.length / 2`

which is 2 and loops to the end of the array copying from`arr2`

to`arr1`

. - [1, 5, 3, -8, 6]
- This would be correct if this loop didn't change
`arr1`

, but it does. - [1, 5, -2, -5, 2]
- This would be correct if it set
`arr1[i]`

equal to`arr[i] + arr[2]`

instead.

7-7-19: Assume that `arr1={1, 5, 3, -8, 6}`

and `arr2={-2, -1, -5, 3, -4}`

what will the contents of `arr1`

be after `copyArray`

finishes executing?

```
public static void copyArray(int[] arr1, int[] arr2)
{
for (int i = arr1.length / 2; i < arr1.length; i++)
{
arr1[i] = arr2[i];
}
}
```

You can step through the code above with the Java Visualizer by clicking the following link Prob-7-10-9.

- The values don't matter this will always cause an infinite loop.
- An infinite loop will not always occur in this code segment.
- Whenever
`a`

includes a value that is less than or equal to zero. - When
`a`

contains a value that is less than or equal to zero then multiplying that value by 2 will never make the result larger than`temp`

(which was set to some value > 0), so an infinite loop will occur. - Whenever
`a`

has values larger then`temp`

. - Values larger then
`temp`

will not cause an infinite loop. - When all values in
`a`

are larger than`temp`

. - Values larger then
`temp`

will not cause an infinite loop. - Whenever
`a`

includes a value equal to`temp`

. - Values equal to
`temp`

will not cause the infinite loop.

7-7-20: Given the following code segment, which of the following will cause an infinite loop? Assume that `temp`

is an `int`

variable initialized to be greater than zero and that `a`

is an array of ints.

```
for ( int k = 0; k < a.length; k++ )
{
while ( a[ k ] < temp )
{
a[ k ] *= 2;
}
}
```

You can step through the code above using the Java Visualizer by clicking on the following link Prob-7-10-10. Can you fix the code so that it won’t result in an infinite loop?

- All values in positions
*m+1*through*myStuff.length-1*are greater than or equal to*n*. - Mystery steps backwards through the array until the first value less than the passed num (
*n*) is found and then it returns the index where this value is found. - All values in position 0 through
*m*are less than*n*. - This would be true if mystery looped forward through the array and returned when it found a value greater than the passed num (
*n*). - All values in position
*m+1*through*myStuff.length-1*are less than*n*. - This would be true if it returned when it found a value at the current index that was greater than num (
*n*). - The smallest value is at position
*m*. - It returns the first time the condition is met so nothing is known about the values which are unchecked.

7-7-21: Given the following array instance variable and method, which of the following best describes the contents of `myStuff`

after (`int m = mystery(n);`

) has been executed?

```
// private field in the class
private int[ ] myStuff;
//precondition: myStuff contains
// integers in no particular order
public int mystery(int num)
{
for (int k = myStuff.length - 1; k >= 0; k--)
{
if (myStuff[k] < num)
{
return k;
}
}
return -1;
}
```

- 4
- This would be correct if the variable col was 0 because then it would add 1 + 1 + 1 + 1 which is 4.
- 8
- Since col is matrix[0].length - 2 it is 4 - 2 which is 2. This code will loop through all the rows and add all the numbers in the third column (index is 2) which is 2 + 2 + 3 + 1 which is 8.
- 9
- This would be correct if the variable col was 1 because then it would add 1 + 2 + 2 + 4 which is 9.
- 12
- This would be correct if the variable col was 3 becuase then it would add 2 + 4 + 4+ 2 which is 12.
- 10
- This would be true if we were adding the values in the 3rd row (row = 2) instead of the 3rd column. This would be 1 + 2 + 3 + 4 which is 10.

7-7-22: Given the following code segment, what is the value of sum after this code executes?

```
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}
```

You can step through this code using the following link Example-9-8-1.

- { {2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}}
- This woud be true if the code put a 3 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 1 in the array when the row index is greater than the column index.
- { {2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}}
- This code will put a 1 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 3 in the array when the row index is greater than the column index.
- { {2 1 1 1}, {3 2 1 1}, {3 3 2 1}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.
- { {2 3 3 3}, {1 2 3 3}, {1 1 2 3}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.
- { {1 1 1 1}, {2 2 2 2}, {3 3 3 3}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.

7-7-23: What are the contents of `mat`

after the following code segment has been executed?

```
int [][] mat = new int [4][3];
for (int row = 0; row < mat.length; row++) {
for (int col = 0; col < mat[0].length; col++) {
if (row < col)
mat[row][col] = 1;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 3; } }
```

You can step through this code using the following link Example-9-8-2.

- 4
- This would be correct if it was adding up all the values in the first row. Does it?
- 6
- This would be correct if it was adding up all the values in column 0.
- 9
- This adds all the values in column 1 starting with the one in the last row (row 3).
- 10
- This would be correct if it was adding up all the values in the second row.
- 20
- This would be correct if it was adding up all the values in the last row.

7-7-24: Given the following code segment, what is the value of sum after this code executes?

```
int[][] m = { {1,1,1,1},{1,2,3,4},{2,2,2,2},{2,4,6,8}};
int sum = 0;
for (int k = 0; k < m.length; k++) {
sum = sum + m[m.length-1-k][1];
}
```

You can step through this code using the following link Example-9-8-3.

## Hard Multiple Choice Questions¶

- Both implementations work as intended and are equally fast.
- Implementation 1 doesn't work and will cause an ArrayIndexOutOfBoundsException. If Implementation 1 was correct, it would be faster.
- Both implementations work as intended, but implementation 1 is faster than implementation 2.
- Implementation 1 doesn't work and will cause an ArrayIndexOutOfBoundsException.
- Both implementations work as intended, but implementation 2 is faster than implementation 1.
- Implementation 1 doesn't work and will cause an ArrayIndexOutOfBoundsException. If it did work, it would be faster than 2.
- Implementation 1 does not work as intended, because it will cause an ArrayIndexOutOfBoundsException.
- When
`j`

is 0,`sum[j-1]`

will be`sum[-1]`

which will cause an ArrayIndexOutOfBoundsException. - Implementation 2 does not work as intended, because it will cause an ArrayIndexOutOfBoundsException.
- Implementation 1 doesn't work and will cause an ArrayIndexOutOfBoundsException.

7-7-25: Consider the following data field and incomplete method, `partialSum`

, which is intended to return an integer array `sum`

such that for all `i`

, `sum[i]`

is equal to `arr[0] + arr[1] + ... + arr[i]`

. For instance, if arr contains the values `{1, 4, 1, 3}`

, the array `sum`

will contain the values `{1, 5, 6, 9}`

. Which of the following is true about the two implementations of `missing code`

on line 9 that are proposed?

```
1private int[] arr;
2
3public int[] partialSum() {
4 int[] sum = new int[arr.length];
5
6 for (int j = 0; j < sum.length; j++)
7 sum[j] = 0;
8
9 /* missing code */
10 return sum;
11}
12
13
14Implementation 1
15
16for (int j = 0; j < arr.length; j++)
17 sum[j] = sum[j - 1] + arr[j];
18
19
20Implementation 2
21
22for (int j = 0; j < arr.length; j++)
23 for (int k = 0; k <= j; k++)
24 sum[j] = sum [j] + arr[k];
```

- { {6, 4, 2}, {2, 4, 6}}
- Check the starting values on the nested loops.
- { {3, 2, 1}, {1, 4, 6}}
- Notice that there are two if's, not an if and else.
- { {3, 2, 1}, {1, 4, 8}}
- The first if will change an odd number to an even. The second if will also execute after an odd number has been made even. Both loops start at index 1 so this only changes the items in the second row and second and third column.
- { {4, 4, 2}, {2, 4, 4}}
- Both if's will execute. Also, check the bounds on the nested loop.
- { {3, 2, 1}, {2, 4, 4}}
- Both if's will execute. Check the bounds on the inner loop. When does it stop?

7-7-26: What are the contents of `arr`

after the following code has been executed?

```
int[][] arr = { {3,2,1},{1,2,3}};
int value = 0;
for (int row = 1; row < arr.length; row++) {
for (int col = 1; col < arr[0].length; col++) {
if (arr[row][col] % 2 == 1)
{
arr[row][col] = arr[row][col] + 1;
}
if (arr[row][col] % 2 == 0)
{
arr[row][col] = arr[row][col] * 2;
}
}
}
```

To step through this code in the Java Visualizer click on the following link: Hard 1.

- The maximum brightness value for all pixels in imagePixel
- The method works by scanning all the pixels in imagePixels and comparing them to the current iMax value. If the current is greater, it replaces iMax and becomes the new maximum brightness. This is the value that is returned.
- The column with the greatest brightness sum
- This could be accomplished by adding the brightness in the second loop and comparing the sum to iMax after the second loop finishes and before the first loop starts again.
- The most frequent brightness value in imagePixels
- To do this you would need a third loop and an array, 256 in size. In the second loop you would track how many pixels of a certain brightness had occurred using, countBright[i]++, and then in the third loop find the item in countBright with the highest value.
- The row with the greatest brightness sum
- Firstly, you would need to traverse the 2D array in the opposite order, going through the rows instead of the columns. Then, you would sum each row's brightness in the second loop and compare it to the max in the first loop.
- The sum of the total brightness of imagePixels
- This would be accomplished by instead of having an if statement to track the currentmax, simply using, sum += imagePixels[r][c];

7-7-27: A two-dimensional array, `imagePixels`

, holds the brightness values for the pixels in an image. The brightness can range from 0 to 255. What does the following method compute?

```
1public int findMax(int[][] imagePixels) {
2 int r, c;
3 int i, iMax = 0;
4
5 for (r = 0; r < imagePixels.length; r++) {
6 for (c = 0; c < imagePixels[0].length; c++) {
7 i = imagePixels[r][c];
8 if (i > iMax)
9 iMax = i;
10 }
11 }
12 return iMax;
13 }
```