Bird
Raised Fist0

Given the following Singleton implementation with eager initialization, what will be the output of the print statement when run?

medium🧾 Code Trace Q4 of Q15
OOP & Design Patterns - Singleton Pattern - Thread Safety, Double-Checked Locking & Lazy Init
Given the following Singleton implementation with eager initialization, what will be the output of the print statement when run?
ATrue
BRaises Exception
CFalse
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Trace instance creation for s1

    _instance is eagerly created and returned for s1.
  2. Step 2: Trace instance creation for s2

    _instance is returned again for s2, same object.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Both s1 and s2 reference the same eagerly created instance [OK]
Quick Trick: Eager init returns same pre-created instance always [OK]
Common Mistakes:
MISTAKES
  • Assuming new instance created on each call
Trap Explanation:
PITFALL
  • Candidates may think __new__ creates new object each time ignoring _instance reuse.
Interviewer Note:
CONTEXT
  • Tests tracing eager singleton behavior on edge case input
Master "Singleton Pattern - Thread Safety, Double-Checked Locking & Lazy Init" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes