Deletions

Practice Questions

Computer Science › Deletions

Questions
3
1

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

2

Which of the following defines a method that successfully deletes an item from an array of integers?

3

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?

Return to subject