← Quiz: Java Concurrency Utilities: all 10 questions
Quiz: Java Concurrency Utilities · Question 2 of 10
What does this print? (code)
What does this print?
import java.util.concurrent.atomic.*;
public class Main {
public static void main(String[] args) {
AtomicInteger a = new AtomicInteger(10);
boolean ok = a.compareAndSet(5, 7);
System.out.println(ok + " " + a.incrementAndGet());
}
}
- A.true 8
- B.false 10
- C.true 11
- D.false 11
Answer & explanation
Correct answer: false 11
The value is 10, not 5, so the CAS fails and nothing changes. incrementAndGet then returns the new value 11.
Can you get the rest right without peeking?
Take the timed Quiz: Java Concurrency Utilities quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →