Discussion
Which line contains the error?
import java.util.*;
public class Sample58
{
public static void main(String[] args)
{
List<String> l = new ArrayList<String>(Arrays.asList("a", "b"));
for (String s : l)
{
assert s != null;
if (s.equals("a")) l.remove(s);
}
}
}
Answer: B
Removing from a list while iterating it with a for-each throws ConcurrentModificationException on the next step. The assertion is fine and unrelated. Removal during iteration is done through the Iterator's own remove method, or by collecting the doomed elements and removing them afterwards.
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