http://javarevisited.blogspot.com/2011/03/10-interview-questions-on-singleton.html
The topic for this week is on singleton patterns, but specifically for real world applications, on common interview questions that could catch any undergraduate students.
Let’s start with an introduction. Singleton pattern is a software design pattern that restricts class instantiations to one object, which implies that it restricts the class to global variables that can be accessed and changed anywhere in the code. Thus, the critics calls it an anti-pattern. Critics argues that it is overused in too many places where it is not beneficial. It introduces unnecessary restrictions where one single instantiation is not required, and it introduces global variables into the application. Thus, singleton patterns complicates the code. So, why is it useful? In Java, singleton design patterns are the most common patterns a software developer will see. It is heavily used in core Java libraries.
The article introduces 10 common interview questions and answers on singleton patterns in Java. I chose it because doing well on technical interviews is one of the most crucial aspect to landing a great career in this industry. This blog post is a learning step that any undergraduate students can reflect upon. We will only list 5 here, however.
The 1st question is the obvious: What is Singleton class? Have you used Singleton before?
The answer is the following: As stated above, Singleton is a class with only one instance. It provides global variables that are instantiated only once and can be accessed through the getInstance() method. The advantage is that it saves memory from having to instantiate the objects every time. Have I used singleton before? My background is mostly data analytics for the sciences. So, no.
The next question is: Which classes are candidates of Singleton? Which kind of class do you make Singleton in Java?
The author of this blog post commented that this is used to check whether or not the candidate has enough experiences with singleton usage or not. Are the candidates familiar with the advantages and disadvantages of singleton in Java?
The answer is the following: Any classes that would be available to the whole application and hold only one instance. An example is the Runtime class, since only one runtime environment is possible. Another is the utility classes like Popup.
The final question is, can you write code for getInstance() method of a Singleton class in Java? This tests the programmer’s ability to understand the code as well as the concept, since there can be many follow-up questions about the code that the candidate had written.
The answer is to not write the code with double checked locking until asked, since it is more complicated. Therefore, the chances of errors are greater. However, for more experienced programmers with deeper knowledge of double checked locking, volatile variable, and lazy loading, why not?
I chose this post as a reflection on interview questions that I could come across in the workplace. One of the most interesting questions I have been asked is what happens to a particle whose speed exceeds or reaches the speed of light. However, even in technical interviews for software development, I have never been asked questions about singleton patterns. So, this blog post was out of curiosity of the different interview questions that I could come across on singleton patterns in the workplace.
From the blog CS@Worcester – Site Title by myxuanonline and used with permission of the author. All other rights reserved by the author.