He was pleased with the results, seeing an increase in the problem solving abilities of his new team members.
Questions from Singleton pattern is very common in Java interviews and good knowledge of how to implement Singleton pattern certainly help. This is also one of my favorite design pattern interview question and has lots of interesting follow-up to dig into detailsthis not only check the knowledge of design pattern but also check coding, multithreading aspect which is very important while working for a real life application.
In this post have listed some of the most common question asked on Singleton pattern during a Java Interview. I have not provided the answers of these questions as they are easily available via google search but if you guys need I can try to modify this tutorial to include answers as well. As promised earlier and having received lot of request for providing answers of these question, I have decided to update this post along with answers.
By the way if you are preparing for interview on Java technology than you can check my collection on Java interview questions and multi-threading interview questions. There are lot of resources in Javarevisited which can help you in your interview preparation.
On the other hand if you are more interested on design pattern tutorials than you can check my post on builder design pattern 10 Interview question on Singleton Pattern in Java Here is my collection of interview questions based upon Singleton design pattern.
They are collected from various Java interviews and highlights key aspects of pattern and where it is broken, if you know how to create thread-safe singletons and different ways to implement this pattern, and pros and cons of each approach.
What is Singleton class? Have you used Singleton before? Singleton is a class which has only one instance in whole application and provides a getInstance method to access the singleton instance. There are many classes in JDK which is implemented using Singleton pattern like java.
Runtime which provides getRuntime method to get access of it and used to get free memory and total memory in Java. Which classes are candidates of Singleton? Which kind of class do you make Singleton in Java?
Here they check whether candidate has enough experience on usage of singleton or not. Any class which you want to be available to whole application and whole only one instance is viable is candidate of becoming Singleton.
One example of this is Runtime classsince on whole java application only one runtime environment can be possible making Runtime Singleton is right decision. Another example is a utility classes like Popup in GUI application, if you want to show popup with message you can have one PopUp class on whole GUI application and anytime just get its instance, and call show with message.
Can you write code for getInstance method of a Singleton class in Java?
Most of the java programmer fail here if they have mugged up the singleton code because you can ask lots of follow-up question based upon the code they have written. I have seen many programmer write Singleton getInstance method with double checked locking but they are not really familiar with the caveat associated with double checking of singleton prior to Java 5.
I have shared code examples of writing singleton classes using enum, using static factory and with double checked locking in my recent post Why Enum Singletons are better in Javaplease see there.
Is it better to make whole getInstance method synchronized or just critical section is enough? Which one you will prefer? 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. This is again related to double checked locking pattern, well synchronization is costly and when you apply this on whole method than call to getInstance will be synchronized and contented.
Singleton pattern is also closely related to factory design pattern where getInstance serves as static factory method. What is lazy and early loading of Singleton and how will you implement it? This is another great Singleton interview 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. As there are many ways to implement Singleton like using double checked locking or Singleton class with static final instance initialized during class loading.
Former is called lazy loading because Singleton instance is created only when client calls getInstance method while later is called early loading because Singleton instance is created when class is loaded into memory.
Give me some examples of Singleton pattern from Java Development Kit?There are some professions where any job interview will involve some specific interview questions about your creativity and your creative thinking skills. Interview questions.
A free inside look at Critical thinking interview questions and process details for other companies - all posted anonymously by interview candidates. Critical-thinking interview questions Use these sample critical-thinking interview questions to discover how candidates evaluate complex situations and if they can reach logical decisions.
Why test candidates’ critical-thinking skills. Critical thinking is a form of reflective reasoning that evaluates information and arguments by applying a range of intellectual skills to reach clear, logical and coherent judgements within a given context.
Try these sample job interview questions to determine a candidate's critical thinking skills. Creative Job Interview Questions to Test Candidates’ Critical Thinking.
Interviews are often just as difficult for the employer as they are for the prospective candidate. It’s difficult to craft job interview questions that evaluate whether or. Summary. What stands between you and that great career move is often an interviewer's toughest questions.
Handle these tricky interview questions with skill and you'll level the interview playing field and enjoy a better chance of getting the job.