Test: Computer Science

Consider the following code:

public static int[] del(int[] a,int delIndex) {

     if(delIndex < 0 || delIndex >= a.length) {

          return null;

     }

     int[] ret = new int[a.length - 1];

     for(int i = 0; i < a.length; i++) {

          if(i != delIndex) {

               ret[i] = a[i];

          }

     }

     return ret;

}

1.

What is the error in the code above?

The use of the array index i is incorrect.

There is a null pointer exception.

The loop is infinite.

The intial conditional (i.e. the if statement) has incorrect logic.

You need to implement a swap for the values.

1/2 questions

0%
Learning Tools by Varsity Tutors