AP Computer Science A › Insertions
public static int[] doWork(int[] arr, int val,int index) {
int[] ret = new int[arr.length + 1];
for(int i = 0; i < index; i++) {
ret[i] = arr[i];
}
ret[index] = val;
for(int i = index + 1; i < ret.length; i++) {
ret[i] = arr[i - 1];
}
return ret;
}
Which of the following is a possible error in the first loop in code above?
I. The array arr
might be indexed out of bounds.
II. The array ret
might be indexed out of bounds.
III. A null pointer exception might occur.
Both I and III
Both I and III
Only I
Only I
I , II, and III
I , II, and III
Only III
Only III
Both II and III
Both II and III
The most obvious possible error is that the array arr might be a null value. You need to check for these kinds of values before using the variables. (If you do arr[0]
on a null value, an exception will be thrown.) In addition, it is possible that a value for index might be given that is too large. Consider if index = 100
but arr
is only 4 elements long. Then, you will have ret
be a 5 value array. When the first loop starts to run, you will go all the way to 99 (or at least attempt to do so) for the index value i
_; h_owever, once you get to ret[4] = arr[4]
, there will be an out of bounds error on arr
, which only has indices 0, 1, 2, and 3. Of course, there could be other problems later on with ret
, but the question only asks about this first loop.
public static int[] doWork(int[] arr, int val,int index) {
int[] ret = new int[arr.length + 1];
for(int i = 0; i < index; i++) {
ret[i] = arr[i];
}
ret[index] = val;
for(int i = index + 1; i < ret.length; i++) {
ret[i] = arr[i - 1];
}
return ret;
}
Which of the following is a possible error in the first loop in code above?
I. The array arr
might be indexed out of bounds.
II. The array ret
might be indexed out of bounds.
III. A null pointer exception might occur.
Both I and III
Only I
I , II, and III
Only III
Both II and III
The most obvious possible error is that the array arr might be a null value. You need to check for these kinds of values before using the variables. (If you do arr[0]
on a null value, an exception will be thrown.) In addition, it is possible that a value for index might be given that is too large. Consider if index = 100
but arr
is only 4 elements long. Then, you will have ret
be a 5 value array. When the first loop starts to run, you will go all the way to 99 (or at least attempt to do so) for the index value i
_; h_owever, once you get to ret[4] = arr[4]
, there will be an out of bounds error on arr
, which only has indices 0, 1, 2, and 3. Of course, there could be other problems later on with ret
, but the question only asks about this first loop.
public static int[] doWork(int[] arr, int val,int index) {
int[] ret = new int[arr.length + 1];
for(int i = 0; i < index; i++) {
ret[i] = arr[i];
}
ret[index] = val;
for(int i = index + 1; i < ret.length; i++) {
ret[i] = arr[i - 1];
}
return ret;
}
Which of the following is a possible error in the first loop in code above?
I. The array arr
might be indexed out of bounds.
II. The array ret
might be indexed out of bounds.
III. A null pointer exception might occur.
Both I and III
Only I
I , II, and III
Only III
Both II and III
The most obvious possible error is that the array arr might be a null value. You need to check for these kinds of values before using the variables. (If you do arr[0]
on a null value, an exception will be thrown.) In addition, it is possible that a value for index might be given that is too large. Consider if index = 100
but arr
is only 4 elements long. Then, you will have ret
be a 5 value array. When the first loop starts to run, you will go all the way to 99 (or at least attempt to do so) for the index value i
_; h_owever, once you get to ret[4] = arr[4]
, there will be an out of bounds error on arr
, which only has indices 0, 1, 2, and 3. Of course, there could be other problems later on with ret
, but the question only asks about this first loop.
public static int[] doWork(int[] arr, int val,int index) {
int[] ret = new int[arr.length + 1];
for(int i = 0; i < index; i++) {
ret[i] = arr[i];
}
ret[index] = val;
for(int i = index + 1; i < ret.length; i++) {
ret[i] = arr[i - 1];
}
return ret;
}
Which of the following is a possible error in the first loop in code above?
I. The array arr
might be indexed out of bounds.
II. The array ret
might be indexed out of bounds.
III. A null pointer exception might occur.
Both I and III
Only I
I , II, and III
Only III
Both II and III
The most obvious possible error is that the array arr might be a null value. You need to check for these kinds of values before using the variables. (If you do arr[0]
on a null value, an exception will be thrown.) In addition, it is possible that a value for index might be given that is too large. Consider if index = 100
but arr
is only 4 elements long. Then, you will have ret
be a 5 value array. When the first loop starts to run, you will go all the way to 99 (or at least attempt to do so) for the index value i
_; h_owever, once you get to ret[4] = arr[4]
, there will be an out of bounds error on arr
, which only has indices 0, 1, 2, and 3. Of course, there could be other problems later on with ret
, but the question only asks about this first loop.
public static int[] doWork(int[] arr, int val,int index) {
int[] ret = new int[arr.length + 1];
for(int i = 0; i < index; i++) {
ret[i] = arr[i];
}
ret[index] = val;
for(int i = index + 1; i < ret.length; i++) {
ret[i] = arr[i - 1];
}
return ret;
}
Which of the following is a possible error in the first loop in code above?
I. The array arr
might be indexed out of bounds.
II. The array ret
might be indexed out of bounds.
III. A null pointer exception might occur.
Both I and III
Only I
I , II, and III
Only III
Both II and III
The most obvious possible error is that the array arr might be a null value. You need to check for these kinds of values before using the variables. (If you do arr[0]
on a null value, an exception will be thrown.) In addition, it is possible that a value for index might be given that is too large. Consider if index = 100
but arr
is only 4 elements long. Then, you will have ret
be a 5 value array. When the first loop starts to run, you will go all the way to 99 (or at least attempt to do so) for the index value i
_; h_owever, once you get to ret[4] = arr[4]
, there will be an out of bounds error on arr
, which only has indices 0, 1, 2, and 3. Of course, there could be other problems later on with ret
, but the question only asks about this first loop.
True or False.
The worst case for insertion into an ArrayList is O(N).
True
False
Insertion into an ArrayList is typically O(1). However, since an ArrayList uses an array as its underlying data structure, there can be a need to resize the underlying array. Resizing an array requires creating a new array and copying all the data from the old array to the new array which takes O(N) time.
True or False.
The worst case for insertion into an ArrayList is O(N).
True
False
Insertion into an ArrayList is typically O(1). However, since an ArrayList uses an array as its underlying data structure, there can be a need to resize the underlying array. Resizing an array requires creating a new array and copying all the data from the old array to the new array which takes O(N) time.
True or False.
The worst case for insertion into an ArrayList is O(N).
True
False
Insertion into an ArrayList is typically O(1). However, since an ArrayList uses an array as its underlying data structure, there can be a need to resize the underlying array. Resizing an array requires creating a new array and copying all the data from the old array to the new array which takes O(N) time.
public static int[] doWork(int[] arr, int val,int index) {
int[] ret = new int[arr.length + 1];
for(int i = 0; i < index; i++) {
ret[i] = arr[i];
}
ret[index] = val;
for(int i = index + 1; i < ret.length; i++) {
ret[i] = arr[i - 1];
}
return ret;
}
What does the code above perform?
It inserts a new value in to an array, adding that value to the existing array
It inserts a new value into an array by overwriting the value that was there before
It deletes a value from the array, so long as it is at a given index
It searches for a value in the array, storing a new value at the index at which that value was found
None of the others answers
A quesiton like this is most easily understood by doing a careful reading of the code in question. Let's consider each major section of code:
int[] ret = new int[arr.length + 1];
This line of code creates a new array, one that is 1 longer than the array arr
.
for(int i = 0; i < index; i++) . . .
This loop copies into ret
the values of arr
up to index - 1
.
(This is because of the i < index
condition)
Then, the code stores the value val
in ret[index]
_for(int i = index + 1; i < ret.length; i++) . . ._
It then finishes copying the values into ret
.
Thus, what this does is insert a value into the original array arr
, returning the new array that is one size larger. (This is necessary because of the static sizing of arrays in Java.)
public static int[] doWork(int[] arr, int val,int index) {
int[] ret = new int[arr.length + 1];
for(int i = 0; i < index; i++) {
ret[i] = arr[i];
}
ret[index] = val;
for(int i = index + 1; i < ret.length; i++) {
ret[i] = arr[i - 1];
}
return ret;
}
What does the code above perform?
It inserts a new value in to an array, adding that value to the existing array
It inserts a new value into an array by overwriting the value that was there before
It deletes a value from the array, so long as it is at a given index
It searches for a value in the array, storing a new value at the index at which that value was found
None of the others answers
A quesiton like this is most easily understood by doing a careful reading of the code in question. Let's consider each major section of code:
int[] ret = new int[arr.length + 1];
This line of code creates a new array, one that is 1 longer than the array arr
.
for(int i = 0; i < index; i++) . . .
This loop copies into ret
the values of arr
up to index - 1
.
(This is because of the i < index
condition)
Then, the code stores the value val
in ret[index]
_for(int i = index + 1; i < ret.length; i++) . . ._
It then finishes copying the values into ret
.
Thus, what this does is insert a value into the original array arr
, returning the new array that is one size larger. (This is necessary because of the static sizing of arrays in Java.)