The answer is the letter B, the "forEach" method does the concatenation and the "replaceAll" method replaces the last concatenation with the argument you pass. I tested the code:
public static void main (String[] args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("Tech");
arrayList.add("Expert");
arrayList.set(0, "Java");
arrayList.forEach(a -> a.concat("Forum"));
arrayList.replaceAll(s -> s.concat("Group"));
System.out.println(arrayList);
}
If you comment the foreach line, you can also get JavaGroup and ExpertGroup. I think replaceAll can make concatenation valid but not foreach:
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("Tech");
arrayList.add("Expert");
arrayList.set(0, "Java");
// arrayList.forEach(a -> a.concat("Forum"));
arrayList.replaceAll(s -> s.concat("Group"));
System.out.println(arrayList);
}
//output: [JavaGroup, ExpertGroup]
forEach iterates over each element in the arrayList, then Concat returns a new string. It does not change the original string in the arrayList.
Replaceall will replace the original string in the arrayList.
upvoted 3 times
...
...
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
akbiyik
1 year, 1 month agoiSnover
1 year, 3 months agojackymak
7 months, 1 week agoDriftKing
4 months ago