exam questions

Exam 1z0-819 All Questions

View all questions & answers for the 1z0-819 exam

Exam 1z0-819 topic 1 question 170 discussion

Actual exam question from Oracle's 1z0-819
Question #: 170
Topic #: 1
[All 1z0-819 Questions]

Given:



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

  • A.
  • B.
  • C.
  • D.
Show Suggested Answer Hide Answer
Suggested Answer: AC 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
aruni_mishra
7 months ago
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
upvoted 1 times
...
cathDev
9 months, 4 weeks ago
Selected Answer: AC
Tested A C
upvoted 1 times
...
ASPushkin
1 year ago
Selected Answer: A
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.
upvoted 1 times
...
d7bb0b2
1 year ago
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
upvoted 1 times
d7bb0b2
1 year ago
I mean say B sorry :D
upvoted 1 times
...
...
ASPushkin
1 year, 2 months ago
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.
upvoted 1 times
d7bb0b2
1 year ago
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.
upvoted 1 times
...
...
Omnisumem
1 year, 3 months ago
Selected Answer: AC
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); } }
upvoted 1 times
d7bb0b2
1 year ago
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
upvoted 1 times
...
...
[Removed]
1 year, 5 months ago
Selected Answer: AC
ArrayList & synchronizedList would throw a ConcurrentModificationException when removing elements while iterating over it.
upvoted 2 times
d7bb0b2
1 year ago
ArrayList not throw error, because is created a copy just for not throw ConcurrentModificationException, So is valid and remove elements for the original list
upvoted 1 times
...
...
Stavok
1 year, 6 months ago
Selected Answer: AC
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.
upvoted 2 times
d7bb0b2
1 year ago
ConcurrentLinkedQueue has a method remove that takes an object as argument!
upvoted 1 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago