AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Debugging Code: Remove All 2's from ArrayList

APCSA-ZAWENY

Consider the following method which was designed to remove all 2’s from the array list:

// remove all the 2's in the array list
public static ArrayList<Integer> removeTwos(ArrayList<Integer> nums)
{
     for(int i=0; i<nums.size(); i++)
     {
         if (nums.get(i)==2)
         {
             nums.remove(2);
             i--;
         }
     }
     return nums;
}

Which of the following array lists would this method FAIL to remove all of the 2's from successfully?

A

[8, 4, 2, 5, 9]

B

[2, 4, 2, 2, 9]

C

[8, 4, 2, 2, 9]

D

[8, 4, 3, 5, 9]

E

The method would remove all of the 2’s in each of these scenarios.