# 6.8. Multiple-Choice Exercises¶

## 6.8.1. Easier Multiple Choice Questions¶

These problems are easier than most of those that you will usually see on the AP CS A exam.

`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.

6-8-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.

6-8-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`

6-8-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.

6-8-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.

6-8-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.

6-8-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.

6-8-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
```

## 6.8.2. Medium Multiple Choice Questions¶

These problems are similar to those you will see on the AP CS A exam.

- 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.

6-8-8: 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`

.

6-8-9: 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.

6-8-10: 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`

.

6-8-11: 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.

6-8-12: 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.

6-8-13: 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.

6-8-14: 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`

.

6-8-15: 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.

6-8-16: 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.

6-8-17: 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.

6-8-18: 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;
}
```

## 6.8.3. Hard Multiple Choice Questions¶

These problems are harder than most of those that you will usually see on the AP CS A exam.

- 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.

6-8-19: 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?

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