Standard Operations & Algorithms - AP Computer Science A
Card 0 of 319
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?
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?
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.)
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.)
Compare your answer with the correct one above
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.
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.
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.
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.
Compare your answer with the correct one above
int\[\] arr = {0,0,0,0,0,0,0,0,0,0};
int arrFill = 0;
int val;
// In here, n items are added. ArrFill is n. Presume that n <= 9
// The variable val now contains a new value to be added
Which of the following blocks of code push a new value into arr as though it were a stack?
int\[\] arr = {0,0,0,0,0,0,0,0,0,0};
int arrFill = 0;
int val;
// In here, n items are added. ArrFill is n. Presume that n <= 9
// The variable val now contains a new value to be added
Which of the following blocks of code push a new value into arr as though it were a stack?
Let us presume that the array arr looks like the following list of integers:
{4,51,41,0,0,0,0,0,0,0}
Presume that our new value is 77.
A stack could either push on to the beginning and move everything back one, giving you something like:
{77,4,51,41,0,0,0,0,0,0}
However, none of the options do this. (No, not even the one that uses the index 0. This one does not add on to the array so much as replace the first element of it.)
So, the other option is to put it on the "end" of the list. The variable arrFill is being used for this purpose. If it is 3, this means that it is the value of the fourth element. Thus, you can set arr\[4\] = 77 (where 4 really is arrFill).
This will give you:
{4,51,41,77,0,0,0,0,0,0}
You also need to add one to the value of arrFill.
The other options do not correctly address the array. They either are too large or too small by one.
Let us presume that the array arr looks like the following list of integers:
{4,51,41,0,0,0,0,0,0,0}
Presume that our new value is 77.
A stack could either push on to the beginning and move everything back one, giving you something like:
{77,4,51,41,0,0,0,0,0,0}
However, none of the options do this. (No, not even the one that uses the index 0. This one does not add on to the array so much as replace the first element of it.)
So, the other option is to put it on the "end" of the list. The variable arrFill is being used for this purpose. If it is 3, this means that it is the value of the fourth element. Thus, you can set arr\[4\] = 77 (where 4 really is arrFill).
This will give you:
{4,51,41,77,0,0,0,0,0,0}
You also need to add one to the value of arrFill.
The other options do not correctly address the array. They either are too large or too small by one.
Compare your answer with the correct one above
True or False.
The worst case for insertion into an ArrayList is O(N).
True or False.
The worst case for insertion into an ArrayList is O(N).
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.
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.
Compare your answer with the correct one above

The above diagram represents what type of sorting algorithm?

The above diagram represents what type of sorting algorithm?
Mergesort consists of breaking down an unsorted list into subarrays; sorting each sub-array, and recombining the sub-arrays into larger arrays.
Mergesort consists of breaking down an unsorted list into subarrays; sorting each sub-array, and recombining the sub-arrays into larger arrays.
Compare your answer with the correct one above
How is merge sort accomplished?
How is merge sort accomplished?
In merge sort, a list is broken up into sublists containing 1 element. Each element is then compared to another element and sorted. Each 2-element group is then combined with other 2-element groups, comparing the first value of each group and deciding how to four elements. The larger group of four is compared to another group of four, until the process ends and the list is sorted.
In merge sort, a list is broken up into sublists containing 1 element. Each element is then compared to another element and sorted. Each 2-element group is then combined with other 2-element groups, comparing the first value of each group and deciding how to four elements. The larger group of four is compared to another group of four, until the process ends and the list is sorted.
Compare your answer with the correct one above
Of the choices below, what is the most efficient sorting algorithm for an unordered list where the size of the list is an odd number and the size of the list is finite?
Of the choices below, what is the most efficient sorting algorithm for an unordered list where the size of the list is an odd number and the size of the list is finite?
Mergesort is the most efficient among the choices. Both selection sort and insertion sort use O(N2) time. Bubble Sort may seem like a good answer but uses O(N2) time most of the time and can be adapted to use O(N) time however only when the list is nearly sorted, so it's a gamble. Mergesort always uses O(NlogN) time and thus is always the most efficient among the four choices.
Mergesort is the most efficient among the choices. Both selection sort and insertion sort use O(N2) time. Bubble Sort may seem like a good answer but uses O(N2) time most of the time and can be adapted to use O(N) time however only when the list is nearly sorted, so it's a gamble. Mergesort always uses O(NlogN) time and thus is always the most efficient among the four choices.
Compare your answer with the correct one above
Which of the following statements explains insertion sort?
Which of the following statements explains insertion sort?
Insertion sort removes an element from a list, checks the values adjacent to it to see if it is greater or smaller, until it finds the position where the number to the left is smaller and the number to the right is larger and places it there.
Insertion sort removes an element from a list, checks the values adjacent to it to see if it is greater or smaller, until it finds the position where the number to the left is smaller and the number to the right is larger and places it there.
Compare your answer with the correct one above
{1, 9, 5, 4, 2, 0, 4}
What would the set of numbers look like after four iterations of Insertion Sort?
{1, 9, 5, 4, 2, 0, 4}
What would the set of numbers look like after four iterations of Insertion Sort?
Insertion Sort is a sorting algorithm that starts at the beginning of an array and with each iteration of the array it sorts the values from smallest to largest.
Therefore, after four iterations of Insertion Sort, the first four numbers will be in order from smallest to largest.
Insertion Sort is a sorting algorithm that starts at the beginning of an array and with each iteration of the array it sorts the values from smallest to largest.
Therefore, after four iterations of Insertion Sort, the first four numbers will be in order from smallest to largest.
Compare your answer with the correct one above
Which of the following do we consider when choosing a sorting algorithm to use?
I. Space efficiency
II. Run time efficiency
III. Array size
IV. Implementation language
Which of the following do we consider when choosing a sorting algorithm to use?
I. Space efficiency
II. Run time efficiency
III. Array size
IV. Implementation language
All of the choices are important when choosing a sorting algorithm. Space and time complexity are the characteristics by which we measure the performance of an algorithm. the array size directly affects the performance of an algorithm. In addition, the language which the algorithm is written in can also affect the performance (for example, insertion sort may run faster in one language versus another, due the way the language was designed).
All of the choices are important when choosing a sorting algorithm. Space and time complexity are the characteristics by which we measure the performance of an algorithm. the array size directly affects the performance of an algorithm. In addition, the language which the algorithm is written in can also affect the performance (for example, insertion sort may run faster in one language versus another, due the way the language was designed).
Compare your answer with the correct one above
Which of the following defines a method that successfully deletes an item from an array of integers?
Which of the following defines a method that successfully deletes an item from an array of integers?
Of course, this is an inefficient way to do such a delete, but arrays are rather "locked" data structures in that their size cannot change without a reassignment. (You could, of course keep track of the last "used" index. However, that is a different implementation, not reflected here.) The correct answer is the one that carefully goes through the original array, copying those contents into the new array skipping the one value that is not wanted.
Of course, this is an inefficient way to do such a delete, but arrays are rather "locked" data structures in that their size cannot change without a reassignment. (You could, of course keep track of the last "used" index. However, that is a different implementation, not reflected here.) The correct answer is the one that carefully goes through the original array, copying those contents into the new array skipping the one value that is not wanted.
Compare your answer with the correct one above
public static boolean remove(int\[\] arr, int val) {
boolean found = false;
int i;
for(i = 0; i < arr.length && !found; i++) {
if(arr\[i\] == val) {
found = true;
}
}
if(found) {
for(int j = i; j < arr.length;j++) {
arr\[j - 1\] = arr\[j\];
}
arr\[arr.length - 1\] = 0;
}
return found;
}
For the code above, what will be the content of the variable arr at the end of execution, if the method is called with the following values for its parameters:
arr = {3,4,4,5,17,4,3,1}
val = 4
public static boolean remove(int\[\] arr, int val) {
boolean found = false;
int i;
for(i = 0; i < arr.length && !found; i++) {
if(arr\[i\] == val) {
found = true;
}
}
if(found) {
for(int j = i; j < arr.length;j++) {
arr\[j - 1\] = arr\[j\];
}
arr\[arr.length - 1\] = 0;
}
return found;
}
For the code above, what will be the content of the variable arr at the end of execution, if the method is called with the following values for its parameters:
arr = {3,4,4,5,17,4,3,1}
val = 4
This code simulates the removal of a value from an array by shifting all of the elements after that one so that the array no longer contains the first instance of that value. So, for instance, this code takes the original array {3,4,4,5,17,4,3,1} and notices the first instance of 4: {3,**4**,4,5,17,4,3,1}. Next, it starts shifting things to the left. Thus, some of the steps will look like this:
{3,4,4,5,17,4,3,1}
{3,4,5,5,17,4,3,1}
{3,4,5,17,17,4,3,1}
...
{3,4,5,5,17,4,1,1}
Then, at the very end, it sets the last element to 0:
{3,4,5,17,4,3,1,0}
This code simulates the removal of a value from an array by shifting all of the elements after that one so that the array no longer contains the first instance of that value. So, for instance, this code takes the original array {3,4,4,5,17,4,3,1} and notices the first instance of 4: {3,**4**,4,5,17,4,3,1}. Next, it starts shifting things to the left. Thus, some of the steps will look like this:
{3,4,4,5,17,4,3,1}
{3,4,5,5,17,4,3,1}
{3,4,5,17,17,4,3,1}
...
{3,4,5,5,17,4,1,1}
Then, at the very end, it sets the last element to 0:
{3,4,5,17,4,3,1,0}
Compare your answer with the correct one above
int\[\] arr = new int\[20\];
int x = 6,i=2;
for(int j = 0; j < x; j++) {
arr\[j\] = j * 40 + 20;
}
for(int j = x; j > i; j--) {
arr\[j\] = arr\[j - 1\];
}
arr\[i\] = 20;
for(int j = 0; j < x; j++) {
System.out.print(arr\[j\] + " ");
}
What is the output for the code above?
int\[\] arr = new int\[20\];
int x = 6,i=2;
for(int j = 0; j < x; j++) {
arr\[j\] = j * 40 + 20;
}
for(int j = x; j > i; j--) {
arr\[j\] = arr\[j - 1\];
}
arr\[i\] = 20;
for(int j = 0; j < x; j++) {
System.out.print(arr\[j\] + " ");
}
What is the output for the code above?
It is easiest to understand this by a bit of code parsing:
First, there is the loop: for(int j = 0; j < x; j++) { // ...
This will fill the array with 20 + 0, 20 + 40, 20 + 80, ... Thus, it will be:
20, 60, 100, 140, 180, 220
Next, there is the array: for(int j = x; j > i; j--) { // ...
This basically shifts everything after index i backward by 1. Thus you have:
20, 60, 100,100, 140, 180, 220
Next, you set arr\[2\] equal to 20:
20, 60, 20,100, 140, 180, 220
Finally you output each of the first 6 elements. Be careful here. Notice that it is from j = 0 to x - 1!
Thus, the answer is:
20 60 20 100 140 180
This algorithm basically implements a simple kind of array deletion.
It is easiest to understand this by a bit of code parsing:
First, there is the loop: for(int j = 0; j < x; j++) { // ...
This will fill the array with 20 + 0, 20 + 40, 20 + 80, ... Thus, it will be:
20, 60, 100, 140, 180, 220
Next, there is the array: for(int j = x; j > i; j--) { // ...
This basically shifts everything after index i backward by 1. Thus you have:
20, 60, 100,100, 140, 180, 220
Next, you set arr\[2\] equal to 20:
20, 60, 20,100, 140, 180, 220
Finally you output each of the first 6 elements. Be careful here. Notice that it is from j = 0 to x - 1!
Thus, the answer is:
20 60 20 100 140 180
This algorithm basically implements a simple kind of array deletion.
Compare your answer with the correct one above
Which of the following statements explains insertion sort?
Which of the following statements explains insertion sort?
Insertion sort removes an element from a list, checks the values adjacent to it to see if it is greater or smaller, until it finds the position where the number to the left is smaller and the number to the right is larger and places it there.
Insertion sort removes an element from a list, checks the values adjacent to it to see if it is greater or smaller, until it finds the position where the number to the left is smaller and the number to the right is larger and places it there.
Compare your answer with the correct one above
{1, 9, 5, 4, 2, 0, 4}
What would the set of numbers look like after four iterations of Insertion Sort?
{1, 9, 5, 4, 2, 0, 4}
What would the set of numbers look like after four iterations of Insertion Sort?
Insertion Sort is a sorting algorithm that starts at the beginning of an array and with each iteration of the array it sorts the values from smallest to largest.
Therefore, after four iterations of Insertion Sort, the first four numbers will be in order from smallest to largest.
Insertion Sort is a sorting algorithm that starts at the beginning of an array and with each iteration of the array it sorts the values from smallest to largest.
Therefore, after four iterations of Insertion Sort, the first four numbers will be in order from smallest to largest.
Compare your answer with the correct one above
Which of the following do we consider when choosing a sorting algorithm to use?
I. Space efficiency
II. Run time efficiency
III. Array size
IV. Implementation language
Which of the following do we consider when choosing a sorting algorithm to use?
I. Space efficiency
II. Run time efficiency
III. Array size
IV. Implementation language
All of the choices are important when choosing a sorting algorithm. Space and time complexity are the characteristics by which we measure the performance of an algorithm. the array size directly affects the performance of an algorithm. In addition, the language which the algorithm is written in can also affect the performance (for example, insertion sort may run faster in one language versus another, due the way the language was designed).
All of the choices are important when choosing a sorting algorithm. Space and time complexity are the characteristics by which we measure the performance of an algorithm. the array size directly affects the performance of an algorithm. In addition, the language which the algorithm is written in can also affect the performance (for example, insertion sort may run faster in one language versus another, due the way the language was designed).
Compare your answer with the correct one above
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?
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?
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.)
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.)
Compare your answer with the correct one above
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.
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.
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.
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.
Compare your answer with the correct one above
int\[\] arr = {0,0,0,0,0,0,0,0,0,0};
int arrFill = 0;
int val;
// In here, n items are added. ArrFill is n. Presume that n <= 9
// The variable val now contains a new value to be added
Which of the following blocks of code push a new value into arr as though it were a stack?
int\[\] arr = {0,0,0,0,0,0,0,0,0,0};
int arrFill = 0;
int val;
// In here, n items are added. ArrFill is n. Presume that n <= 9
// The variable val now contains a new value to be added
Which of the following blocks of code push a new value into arr as though it were a stack?
Let us presume that the array arr looks like the following list of integers:
{4,51,41,0,0,0,0,0,0,0}
Presume that our new value is 77.
A stack could either push on to the beginning and move everything back one, giving you something like:
{77,4,51,41,0,0,0,0,0,0}
However, none of the options do this. (No, not even the one that uses the index 0. This one does not add on to the array so much as replace the first element of it.)
So, the other option is to put it on the "end" of the list. The variable arrFill is being used for this purpose. If it is 3, this means that it is the value of the fourth element. Thus, you can set arr\[4\] = 77 (where 4 really is arrFill).
This will give you:
{4,51,41,77,0,0,0,0,0,0}
You also need to add one to the value of arrFill.
The other options do not correctly address the array. They either are too large or too small by one.
Let us presume that the array arr looks like the following list of integers:
{4,51,41,0,0,0,0,0,0,0}
Presume that our new value is 77.
A stack could either push on to the beginning and move everything back one, giving you something like:
{77,4,51,41,0,0,0,0,0,0}
However, none of the options do this. (No, not even the one that uses the index 0. This one does not add on to the array so much as replace the first element of it.)
So, the other option is to put it on the "end" of the list. The variable arrFill is being used for this purpose. If it is 3, this means that it is the value of the fourth element. Thus, you can set arr\[4\] = 77 (where 4 really is arrFill).
This will give you:
{4,51,41,77,0,0,0,0,0,0}
You also need to add one to the value of arrFill.
The other options do not correctly address the array. They either are too large or too small by one.
Compare your answer with the correct one above
True or False.
The worst case for insertion into an ArrayList is O(N).
True or False.
The worst case for insertion into an ArrayList is O(N).
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.
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.
Compare your answer with the correct one above