CS FundamentalsHardProduct-company MCQ pattern

Assertion (A): A lazily initialised Singleton that checks if (instance == null) before creating the object can end up with two instances in a multithreaded program. Reason (R): Two threads can both see instance as null before either has assigned it.

  1. A. Both A and R are true and R is the correct explanation of A
  2. B. Both A and R are true but R is not the correct explanation of A
  3. C. A is true but R is false
  4. D. A is false but R is true

Correct answer: A. Both A and R are true and R is the correct explanation of A

Without synchronisation the check-then-create sequence is a race condition: both threads pass the null check and each creates an object. R is the exact cause; fixes include eager initialisation, a synchronised accessor, double-checked locking with volatile, or the holder-class idiom.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic