Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 71

Given:

What is the output?

    Correct Answer: D

    The CopyOnWriteArrayList in Java works by making a fresh copy of the underlying array every time a modification is made. When the modification happens, the iterator in the enhanced for loop, which is iterating over the old array, will not see the changes. Thus, the main thread will print the original list items ('1', '2', '3', '4') since it started iterating before any change occurred. Meanwhile, the runnable modifies the list and prints it after 150 milliseconds. This results in both threads printing different versions of the list. Therefore, the final output where '1 2 [1, 2, 3, four] 3 4' is produced is accurate. Option D captures this behavior correctly.

Discussion
KMLAOption: D

D is correct answer

d7bb0b2Option: D

is a dificult question, if run normaly D is correct, but if run with debug option step by step can modify answers because the time in breackpoint can modified output. But The main method in the Example class is creating a concurrent list cp with the elements "1", "2", "3", "4". Then, it creates a Runnable that sleeps for 150 milliseconds, changes the fourth element of the list to "four", and prints the list. After that, it creates a new thread t with the Runnable and starts it. While the thread t is sleeping and before it changes the fourth element of the list, the main thread enters a loop and starts printing the elements of the list one by one, sleeping 100 milliseconds between each print. Therefore, it's likely that the main thread prints the first elements of the list before the thread t gets a chance to change the fourth element. However, the exact behavior may vary depending on the operating system's thread scheduler and when exactly the threads are scheduled to run.

d7bb0b2

NOTICE: foreach print 3, 4 because CopyOnWriteArrayList when is currently itering, this iterations is on a copy of original list, not in the same list. So when change 4 by four no reflected in the copy that iterate.

Mukes877Option: D

D is right because Main thread will also print data and r thread is also print data and those data will be different from each other.

ASPushkinOption: A

answer : A Enhanced loop is actually using itterator under the hood. According to the Java API documentation [API 2014]: It is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. But acording to sleep time delay the non-thread loop itterator will start earlier. But itettrator works directly with arraylist and itterator has to reflect the data modification. Answer A is more preferable.

mendjijetOption: D

D tested

OmnisumemOption: D

Tested: D.

StavokOption: D

D is Correct

tmuralimanoharOption: D

Answer: D