Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 10: Recursion
Start Practice TestPractice Test
About Exam
hard

Volatile Keyword Guarantees in Java

Mark
< Prev
Next >

In the VolatileRecursive class shown below, the count variable is declared as volatile. Which statement best describes the guarantees and limitations of this keyword in the context of the recursiveIncrement method?

public class VolatileRecursive {
    private static volatile int count = 0;
    
    public static void recursiveIncrement(int n) {
        if(n <= 0) return;
        count = count + 1;
        new Thread(() -> recursiveIncrement(n - 1)).start();
    }
    
    public static void main(String[] args) throws InterruptedException {
        recursiveIncrement(5);
        Thread.sleep(1000);
        System.out.println("Count: " + count);
    }
}
A

Volatile prevents any concurrency issues by serializing access to the variable.

B

Volatile guarantees visibility of the variable across threads but does not ensure atomicity of compound operations like incrementing.

C

Volatile has no effect in a recursive method as thread communications are not involved.

D

Volatile guarantees both visibility and atomicity, ensuring that the count is correctly incremented.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|