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

7-10-1: 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-10-2: 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-10-3: 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-10-4: 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-10-5: 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-10-6: 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-10-7: 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-10-8: 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-10-9: 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-10-10: 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?