Topic: 1z0-819 topic 1 question 170
Given:
Which two code fragments remove the elements from the original list? (Choose two.)
A.
B.
C.
D.
IT Certification exam information exchange, brain dumps discussions sharing.
You are not logged in. Please login or register.
Share Test → Other → 1z0-819 topic 1 question 170
Given:
Which two code fragments remove the elements from the original list? (Choose two.)
A.
B.
C.
D.
Tested A C
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.
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
I mean say B sorry
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.
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.
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);
}
}
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
ArrayList & synchronizedList would throw a ConcurrentModificationException when removing elements while iterating over it.
ArrayList not throw error, because is created a copy just for not throw ConcurrentModificationException, So is valid and remove elements for the original list
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.
ConcurrentLinkedQueue has a method remove that takes an object as argument!
Share Test → Other → 1z0-819 topic 1 question 170
Note: This forum is a platform for users to share insights and discuss exam-related topics. We do not provide authentic exam questions or answers. The content here is contributed by community members and is meant for collaborative learning and discussion purposes only. Users are encouraged to refer to official sources for accurate exam materials.