Discussion
Sample71 is run - what will be the output?
import java.util.*;
import java.util.concurrent.*;
public class Sample71
{
public static void main(String[] args)
{
List<String> names = new CopyOnWriteArrayList<>(Arrays.asList("a", "b"));
for (String s : names)
{
names.add("c");
System.out.print(s);
}
System.out.print(" " + names.size());
}
}
Answer: C
The iterator works from a snapshot taken when iteration began, so it visits only a and b however many elements are added, and no ConcurrentModificationException is possible. The two additions do take effect, so the final size is four.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development