Consider the following method. // Replace every element that contains \"bad\" wi
ID: 3727986 • Letter: C
Question
Consider the following method. // Replace every element that contains "bad" with "happy thoughts" public static ArrayList replaceBad (ArrayList phrases) for (String aPhrase: phrases) if (aPhrase.indexOf("bad") >= 0) phrases.set(i, "happy thoughts") return phrases What will print to the console after the following code is run? ArrayList phrases new Array List (); phrases.add("good day" phrases.add ("bad hair"); phrases.add ("cookies are good" phrases.add ("bad apples") replaceBad (phrases); Sy stem.out.print (phrases);Explanation / Answer
Answer: [good day, happy thoughts, cookies are good, happy thoughts]
In ArrayList, second and fourth elements contain bad word. so these will be replaced by "happy thoughts"
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.