AP Computer Science A › Deletions
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
{3, 4, 5, 17, 4, 3, 1, 0}
{3, 5, 17, 3, 1, 0, 0, 0}
{3, 4, 5, 17, 4, 3, 1}
{3, 5, 17, 3, 1}
None of the other answers
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}
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
{3, 4, 5, 17, 4, 3, 1, 0}
{3, 5, 17, 3, 1, 0, 0, 0}
{3, 4, 5, 17, 4, 3, 1}
{3, 5, 17, 3, 1}
None of the other answers
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}
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
{3, 4, 5, 17, 4, 3, 1, 0}
{3, 4, 5, 17, 4, 3, 1, 0}
{3, 5, 17, 3, 1, 0, 0, 0}
{3, 5, 17, 3, 1, 0, 0, 0}
{3, 4, 5, 17, 4, 3, 1}
{3, 4, 5, 17, 4, 3, 1}
{3, 5, 17, 3, 1}
{3, 5, 17, 3, 1}
None of the other answers
None of the other answers
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}
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
{3, 4, 5, 17, 4, 3, 1, 0}
{3, 5, 17, 3, 1, 0, 0, 0}
{3, 4, 5, 17, 4, 3, 1}
{3, 5, 17, 3, 1}
None of the other answers
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}
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
{3, 4, 5, 17, 4, 3, 1, 0}
{3, 5, 17, 3, 1, 0, 0, 0}
{3, 4, 5, 17, 4, 3, 1}
{3, 5, 17, 3, 1}
None of the other answers
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}
Which of the following defines a method that successfully deletes an item from an array of integers?
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 != delIndex) {
ret\[i2\] = a\[i1\];
i2++;
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 == delIndex) {
delete a\[i1\];
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
for(int i = 0; i <= delIndex; i++) {
if(i == delIndex) {
delete a\[i\];
break;
}
}
return a;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i = 0; i <= delIndex; i++) {
ret\[i\] = a\[i\];
}
return ret;
}
None of these work correctly
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.
Which of the following defines a method that successfully deletes an item from an array of integers?
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 != delIndex) {
ret\[i2\] = a\[i1\];
i2++;
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 == delIndex) {
delete a\[i1\];
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
for(int i = 0; i <= delIndex; i++) {
if(i == delIndex) {
delete a\[i\];
break;
}
}
return a;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i = 0; i <= delIndex; i++) {
ret\[i\] = a\[i\];
}
return ret;
}
None of these work correctly
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.
Which of the following defines a method that successfully deletes an item from an array of integers?
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 != delIndex) {
ret\[i2\] = a\[i1\];
i2++;
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 == delIndex) {
delete a\[i1\];
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
for(int i = 0; i <= delIndex; i++) {
if(i == delIndex) {
delete a\[i\];
break;
}
}
return a;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i = 0; i <= delIndex; i++) {
ret\[i\] = a\[i\];
}
return ret;
}
None of these work correctly
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.
Which of the following defines a method that successfully deletes an item from an array of integers?
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 != delIndex) {
ret\[i2\] = a\[i1\];
i2++;
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 == delIndex) {
delete a\[i1\];
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
for(int i = 0; i <= delIndex; i++) {
if(i == delIndex) {
delete a\[i\];
break;
}
}
return a;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i = 0; i <= delIndex; i++) {
ret\[i\] = a\[i\];
}
return ret;
}
None of these work correctly
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.
Which of the following defines a method that successfully deletes an item from an array of integers?
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 != delIndex) {
ret\[i2\] = a\[i1\];
i2++;
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 != delIndex) {
ret\[i2\] = a\[i1\];
i2++;
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 == delIndex) {
delete a\[i1\];
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i1=0,i2 = 0; i1 < a.length; i1++) {
if(i1 == delIndex) {
delete a\[i1\];
}
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
for(int i = 0; i <= delIndex; i++) {
if(i == delIndex) {
delete a\[i\];
break;
}
}
return a;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
for(int i = 0; i <= delIndex; i++) {
if(i == delIndex) {
delete a\[i\];
break;
}
}
return a;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i = 0; i <= delIndex; i++) {
ret\[i\] = a\[i\];
}
return ret;
}
public static int\[\] del(int\[\] a,int delIndex) {
if(a == null || delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i = 0; i <= delIndex; i++) {
ret\[i\] = a\[i\];
}
return ret;
}
None of these work correctly
None of these work correctly
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.