Your question is Spot the ConcurrentModification Bug. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
ServiceNow's ticket queue has a small cleanup routine that removes a closed ticket ID from an in-memory list while walking it. QA flagged that it throws under certain conditions during a test run.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TicketQueueCleaner {
public static void main(String[] args) {
List ticketIds = new ArrayList();
ticketIds.add(101);
ticketIds.add(102);
ticketIds.add(103);
ticketIds.add(104);
Iterator iterator = ticketIds.iterator();
while (iterator.hasNext()) {
Integer id = (Integer) iterator.next();
if (id.equals(102)) {
ticketIds.remove(id);
}
}
System.out.println("Remaining tickets: " + ticketIds);
}
}
Explain exactly what's wrong with this code and what behavior it causes when it runs.