Test: AP Computer Science A

1.

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 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;

     }

     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 i1=0,i2 = 0; i1 < a.length; i1++) {

          if(i1 == delIndex) {

               delete a[i1];

          }

     }

     return ret;

}

None of these work correctly

1/12 questions

0%

Access results and powerful study features!

Take 15 seconds to create an account.
Start now! Create your free account and get access to features like:
  • Full length diagnostic tests
  • Invite your friends
  • Access hundreds of practice tests
  • Monitor your progress over time
  • Manage your tests and results
  • Monitor the progress of your class & students
By clicking Create Account you agree that you are at least 13 years old and you agree to the Varsity Tutors LLC Terms of Use and Privacy Policy.
Learning Tools by Varsity Tutors