Singleton Pattern

Question :what is Singleton? Have you used Singleton before?

Answer :Singleton is a class which has only one instance thought out the application and provides a getInstance() method to access the singleton instance.

Question : Which classes are candidates of Singleton? Which kind of class do you make Singleton in Java?
Answer :Here they will check whether candidate has enough experience on usage of singleton or not. Does he is familiar of advantage/disadvantage or alternatives available for singleton in Java or not.

Question : Can you write code for getInstance() method of a Singleton class in Java?
Answer :Most of the guys fail here if they have mugged up the singleton code because you can ask lots of follow-up question based upon the code written by candidate. In my experience I have seen mostly java interviewee right Singleton getInstance() method with double checking but they are not really familiar with the caveat associated with double checking of singleton prior to Java 5.

Question : Is it better to make whole getInstance() method synchronized or just critical section is enough? Which one you will prefer?
Answer :This is really nice question and I mostly asked to just quickly check whether candidate is aware of performance trade off of unnecessary locking or not. Since locking only make sense when we need to create instance and rest of the time its just read only access so locking of critical section is always better option.

Question : What is lazy and early loading of Singleton and how will you implement it?
Answer :This is again great question in terms of understanding of concept of loading and cost associated with class loading in Java. Many of which I have interviewed not really familiar with this but its good to know concept.

Question : Example of Singleton in standard JAVA Development Kit?
Answer :This is open question to all, please share which classes are Singleton in JDK.

Question : What is double checked locking in Singleton?
Answer :One of the most hyped question on Singleton and really demands complete understanding to get it right because of Java Memory model caveat prior to Java 5. If a guy comes up with a solution of using volatile instance of Singleton then it really shows it has in depth knowledge of Java memory model and he is constantly updating his Java knowledge.

Question : How do you prevent for creating another instance of Singleton using clone() method?
Answer :This type of questions generally comes some time by asking how to break singleton or when Singleton is not Singleton in Java.

Question : How do you prevent for creating another instance of Singleton using reflection?
Answer :Open to all. In my opinion throwing exception from constructor is an option.

Question : How do you prevent for creating another instance of Singleton during serialization?
Answer :Another great question which requires knowledge of Serialization in Java and how to use it for persisting Singleton classes. This is open to you all but in my opinion use of readResolve() method can sort this out for you.

Leave a Reply