volatile Keyword — Concept Check
Questions on volatile Keyword. volatile guarantees memory visibility across threads — changes to a volatile variable are immediately visible to all other threads.
Questions in this quiz
7 questions on volatile Keyword — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer notices that a thread is not seeing updates to a shared boolean flag. What could solve this issue?
- AUse a local variable
- BDeclare the flag as volatile
- CAdd a print statement
- DIncrease the thread priority
2.In a multithreaded Java application, a counter is incremented by multiple threads. Why might using volatile not prevent race conditions?
- AVolatile only ensures visibility, not atomicity
- BVolatile prevents all race conditions
- CVolatile makes operations atomic
- DVolatile locks the variable
3.A developer uses volatile for a flag and synchronized for a counter. Why is this a correct approach?
- AVolatile makes both operations atomic
- BVolatile is used for all variables
- CSynchronized is faster than volatile
- DVolatile ensures visibility; synchronized ensures atomicity
4.Why might a loop in a thread never terminate even after a flag is set to true in another thread?
- AThe flag is declared as volatile
- BThe thread is sleeping
- CThe loop condition is incorrect
- DThe flag is not declared as volatile
5.A singleton pattern uses double-checked locking. Why is volatile necessary for the instance variable?
- AVolatile prevents partial initialization
- BVolatile speeds up access
- CVolatile makes the instance immutable
- DVolatile allows multiple instances
6.What happens when a volatile variable is written to by one thread and read by another?
- AThe write happens-before the read
- BThe operations are independent
- CBoth operations are atomic
- DThe read happens-before the write
7.In Java, what does the volatile keyword guarantee?
- AMemory visibility across threads
- BAtomicity of operations
- CFaster execution
- DThread safety for all operations