🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Concurrency Utilities: all 10 questions

Quiz: Java Concurrency Utilities · Question 8 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) {
        AtomicLong a = new AtomicLong(5);
        long x = a.getAndAdd(3);
        long y = a.addAndGet(3);
        System.out.println(x + " " + y);
    }
}
  1. A.8 11
  2. B.5 8
  3. C.5 11
  4. D.8 8
Answer & explanation

Correct answer: 5 11

getAndAdd returns the old value (5) and makes it 8; addAndGet returns the new value, 8 + 3 = 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 →