?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Enhanced For Loop: Using ArrayList Set Method

APCSA-L4T6EY

Consider the following method.

// Replace every element that contains "bad" with "happy thoughts"
public static ArrayList<String> replaceBad(ArrayList<String> phrases)
{
     int i = 0;
     for (String aPhrase : phrases) 
     {
          if (aPhrase.indexOf("bad") >= 0)
          {
               phrases.set(i, "happy thoughts");
          }
          i++;
     }
     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");
replaceBad(phrases);
System.out.print (phrases);
A

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

B

[good day, happy thoughts, cookies are good, happy thoughts]

C

[good day, happy thoughts, cookies are good, bad apples]

D

[good day, bad hair, cookies are good, happy thoughts]

E

An error will be thrown.