This book is now obsolete Please use CSAwesome instead.
8.11. 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 besum[-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-11-1: 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];
|
You have attempted of activities on this page