1z0-819 Exam QuestionsBrowse all questions from this exam

1z0-819 Exam - Question 170


Given:

Which two code fragments remove the elements from the original list? (Choose two.)

Show Answer
Correct Answer: ABCD

To remove elements from the original list without causing a ConcurrentModificationException, we must avoid modifying the list directly while iterating over it with a for-each loop. The best way to achieve this is by ensuring that the list modifications are thread-safe or handled in such a way that doesn’t lead to concurrent issues. ConcurrentLinkedQueue (option A) allows thread-safe operations and does not throw a ConcurrentModificationException during removal while iterating. Using Collections.synchronizedList (option D) manually synchronizes the iteration and removal operations, preventing concurrent access issues. Hence, these two options successfully remove elements from the original list.

Discussion

8 comments
Sign in to comment
StavokOptions: AC
Jul 22, 2023

C creates a new CopyOnWriteArrayList from the original list and then iterates over it, removing each element. The CopyOnWriteArrayList class is thread-safe and allows for concurrent modification, so it is safe to remove elements while iterating over it. Option A is incorrect because ConcurrentLinkedQueue does not have a remove method that takes an object as an argument.

d7bb0b2
Jan 9, 2024

ConcurrentLinkedQueue has a method remove that takes an object as argument!

[Removed]Options: AC
Aug 31, 2023

ArrayList & synchronizedList would throw a ConcurrentModificationException when removing elements while iterating over it.

d7bb0b2
Jan 9, 2024

ArrayList not throw error, because is created a copy just for not throw ConcurrentModificationException, So is valid and remove elements for the original list

OmnisumemOptions: AC
Oct 12, 2023

Tested AC. With a lot of modification...: import java.util.*; import java.util.concurrent.*; public class q170 { public static void main(String[] args) { List<Integer> original = new ArrayList<>(Arrays.asList(1,2,3,4,5)); System.out.println(original); Queue<Integer> clq = new ConcurrentLinkedQueue<>(original); for (Integer w : clq) {clq.remove(w);} System.out.println(clq); //List<Integer> al = new ArrayList<>(original); //for (Integer w : al) {al.remove(w);} //System.out.println(al); List<Integer> cwa = new CopyOnWriteArrayList<>(original); for (Integer w : cwa) {cwa.remove(w);} System.out.println(cwa); //List<Integer> sl = Collections.synchronizedList(original); //for (Integer w : sl) {sl.remove(w);} //System.out.println(sl); } }

d7bb0b2
Jan 9, 2024

THat is incorect you print cwa in line CopyOnWriteArrayList ... must be print original for know if list original is empty. if you print original System.out.println(cwa); =>[] System.out.println(original);=> [1, 2, 3, 4, 5] then option With concurrent-queue-arrayslist work in a copy not in the original list. SO THAT OPTIONS ARE INVALID for remove items from original list

ASPushkinOptions: AD
Nov 22, 2023

answer: A and maybe D A : ConcurrentLinkedQueue is ok because it will never throw any ConcurrentModificationException, even if you remove or add item. Note that those are all in java.util.concurrent package. B: For-each loops are not appropriate when you want to modify the ArrayList. Iterator should be used. C: is not good. cwa - new collection with new copy of the elements. So, it doesn't remove anything from the original. D: looks like it can work if put synchronized(al) before loop. Should be checked.

d7bb0b2
Jan 9, 2024

A is correct, can modified original list if you create other arraylist from the original list. D: only work if create a copy of syncronizedliss as follow: List<Integer> sl = Collections.synchronizedList(list); synchronized (sl) { List<Integer> copyList = new ArrayList<>(sl); for (Integer i : copyList) { sl.remove(i); } } or else thown exception.

d7bb0b2Option: A
Jan 9, 2024

Only correct is A, ConcurrentLinkedQueue and CopyOnWriteArrayList=> WORK A COPY not reference to list so elemments in original list are not removed Collections.synchronizedList => thows excpetion

d7bb0b2
Jan 9, 2024

I mean say B sorry :D

ASPushkinOption: A
Feb 4, 2024

A : ConcurrentLinkedQueue is ok because it will never throw any ConcurrentModificationException, even if you remove or add item. Note that those are all in java.util.concurrent package.

cathDevOptions: AC
Apr 11, 2024

Tested A C

aruni_mishra
Jul 4, 2024

Note: question ask which code fragments remove the elements from the "original list". answer is None. if question would has been which code compiles, then answer is A and C. because "for-each" loop use Iterator, and with regular list, we get : ConcurrentModificationException