?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Enhanced For Loop: Removing an Element from ArrayList

APCSA-XYNETE

Consider the following method.

// Remove every element that contains "bad"
public static ArrayList<String> removeBad(ArrayList<String> phrases)
{
     for (String aPhrase : phrases)
     {
          if (aPhrase.indexOf("bad") >= 0)
          {
               phrases.remove(aPhrase);
          }
     }
     return phrases;
}

What will print to the console after the following code is run?

ArrayList<String> phrases = new ArrayList<String> ();
phrases.add("good day");
phrases.add("bad hair");
phrases.add("cookies are good");
phrases.add("bad apples");
removeBad(phrases);
System.out.print(phrases);
A

[good day, bad hair, cookies are good, bad apples]

B

[good day, cookies are good]

C

[good day, cookies are good, bad apples]

D

[good day, bad hair, cookies are good]

E

An error will be thrown.