What is the result?

Given:

import java.util.concurrent.atomic.AtomicInteger;

public class Incrementor {

public static void main(String[] args) {

AtomicInteger[] var = new AtomicInteger[5];

for (int i = 0; i < 5; i++) {

var[i] = new AtomicInteger();

}

for (int i =0; i < var.length; i++) {

var[i].incrementAndGet();

if (i ==2)

var[i].compareAndSet(2,4);

System.out.print(var[i] + ” “);

}

}

}

What is the result?

Given:

import java.util.concurrent.atomic.AtomicInteger;

public class Incrementor {

public static void main(String[] args) {

AtomicInteger[] var = new AtomicInteger[5];

for (int i = 0; i < 5; i++) {

var[i] = new AtomicInteger();

}

for (int i =0; i < var.length; i++) {

var[i].incrementAndGet();

if (i ==2)

var[i].compareAndSet(2,4);

System.out.print(var[i] + ” “);

}

}

}

What is the result?

A.
1 1 1 1 1

B.
1 2 3 4 5

C.
0 1 2 3 4

D.
0 1 4 3 4



Leave a Reply 6

Your email address will not be published. Required fields are marked *


Charlie

Charlie

final public boolean compareAndSet(int expect, int update)
Atomically sets the value to the given updated value if the current value == the expected value.

gelete

gelete

A.

Output: 1 1 1 1 1

gelete

gelete

sorry

Output:
0 0 0 0 0
1 1 1 1 1

gelete

gelete

sorry, same question in 1Z0-804 V.2 and V.1 has System.out in the first loop

for (int i = 0; i < 5; i++) {
var[i] = new AtomicInteger();
System.out.print(var[i] + " ");
}
System.out.println();

Here
Output: 1 1 1 1 1

In V.2, V.1 :
Output:
0 0 0 0 0
1 1 1 1 1