10 Singleton Designing Interview Questions Inwards Coffee - Answered

Singleton pattern pattern is i of the most mutual patterns you lot volition meet inwards Java applications together with it’s too used heavily inwards gist Java libraries. Questions from Singleton pattern is really mutual inwards Java interviews together with skilful noesis of how to implement Singleton pattern sure as shooting help.This is too i of my favorite design pattern interview question together with has lots of interesting follow-up to dig into details , this non alone cheque the noesis of pattern pattern but too cheque coding, multithreading appear which is really of import spell working for a existent life application. In this postal service have got listed only about of the most mutual query asked on Singleton pattern during a Java Interview. I have got non provided the answers of these questions equally they are easily available via google search but if you lot guys demand I tin shipping away effort to modify this tutorial to include answers equally well.


As promised before together with having received lot of asking for providing answers of these question, I have got decided to update this postal service along amongst answers. By the agency if you lot are preparing for interview on Java technology scientific discipline than you lot tin shipping away cheque my collection on Java interview questions together with multi-threading interview questions.

There are lot of resources inwards which tin shipping away assist you lot inwards your interview preparation. On the other manus if you lot are to a greater extent than interested on pattern pattern tutorials than you lot tin shipping away cheque my postal service on builder pattern pattern



10 Interview query on Singleton Pattern inwards Java

Here is my collection of interview questions based upon Singleton pattern pattern. They are collected from diverse Java interviews together with highlights primal aspects of pattern together with where it is broken, if you lot know how to do thread-safe singletons together with dissimilar ways to implement this pattern, together with pros together with cons of each approach. Questions starts amongst :


What is Singleton class? Have you lot used Singleton before?

Singleton is a degree which has alone i illustration inwards whole application together with provides a getInstance() method to access the singleton instance. There are many classes inwards JDK which is implemented using Singleton pattern similar java.lang.Runtime which provides getRuntime() method to acquire access of it together with used to acquire free retention together with full retention inwards Java.


Which classes are candidates of Singleton? Which sort of degree do you lot brand Singleton inwards Java?

Here they cheque whether candidate has plenty sense on usage of singleton or not. Does he is familiar of advantage/disadvantage or alternatives available for singleton inwards Java or not.

Answer : Any degree which you lot desire to live on available to whole application together with whole alone i illustration is feasible is candidate of becoming Singleton. One illustration of this is Runtime degree , since on whole coffee application alone i runtime environs tin shipping away live on possible making Runtime Singleton is correct decision. Another illustration is a utility classes similar Popup inwards GUI application, if you lot desire to demonstrate popup amongst message you lot tin shipping away have got i PopUp degree on whole GUI application together with anytime only acquire its instance, together with telephone outcry upward show() amongst message.


Can you lot write code for getInstance() method of a Singleton degree inwards Java?

Most of the coffee programmer neglect hither if they have got mugged upward the singleton code because you lot tin shipping away enquire lots of follow-up query based upon the code they have got written. I have got seen many programmer write Singleton getInstance() method amongst double checked locking but they are non actually familiar amongst the caveat associated amongst double checking of singleton prior to Java 5.

Answer : Until asked don’t write code using double checked locking equally it is to a greater extent than complex together with chances of errors are to a greater extent than but if you lot have got deep noesis of double checked locking, volatile variable together with lazy loading than this is your run a hazard to shine. I have got shared code examples of writing singleton classes using enum, using static mill together with amongst double checked locking inwards my recent postal service Why Enum Singletons are amend inwards Java, delight meet there.

 Singleton pattern pattern is i of the most mutual patterns you lot volition meet inwards Java applicat 10 Singleton Pattern Interview Questions inwards Java - Answered


Is it amend to brand whole getInstance() method synchronized or only critical department is enough? Which i you lot volition prefer?

This is actually overnice query together with I mostly asked to only speedily cheque whether candidate is aware of functioning merchandise off of unnecessary locking or not. Since locking alone brand sense when nosotros demand to do illustration together with balance of the fourth dimension its only read alone access hence locking of critical department is ever amend option. read to a greater extent than nigh synchronization on How Synchronization industrial plant inwards Java
Answer : This is over again related to double checked locking pattern, good synchronization is costly together with when you lot apply this on whole method than telephone outcry upward to getInstance() volition live on synchronized together with contented. Since synchronization is alone needed during initialization on singleton instance, to preclude creating only about other illustration of Singleton, It’s amend to alone synchronize critical department together with non whole method. Singleton pattern is too closely related to factory pattern pattern where getInstance() serves equally static mill method.



What is lazy together with early on loading of Singleton together with how volition you lot implement it?

This is only about other slap-up Singleton interview query inwards terms of agreement of concept of loading together with terms associated amongst degree loading inwards Java. Many of which I have got interviewed non actually familiar amongst this but its skilful to know concept.

Answer : As in that place are many ways to implement Singleton similar using double checked locking or Singleton degree amongst static final illustration initialized during degree loading. Former is called lazy loading because Singleton illustration is created alone when customer calls getInstance() method spell subsequently is called early on loading because Singleton illustration is created when degree is loaded into memory.



Give me only about examples of Singleton pattern from Java Development Kit?

This is opened upward query to all, delight part which classes are Singleton inwards JDK. Answer to this query is java.lang.Runtime
Answer : There are many classes inwards Java Development Kit which is written using singleton pattern, hither are few of them:
  1. Java.lang.Runtime amongst getRuntime() method 
  2. Java.awt.Toolkit amongst getDefaultToolkit() 
  3. Java.awt.Desktop amongst getDesktop() 



What is double checked locking inwards Singleton?

One of the most hyped query on Singleton pattern together with actually demands consummate agreement to acquire it correct because of Java Memory model caveat prior to Java 5. If a guy comes upward amongst a solution of using volatile keyword amongst Singleton illustration together with explains it hence it actually shows it has inwards depth noesis of Java retention model together with he is constantly updating his Java knowledge.

Answer : Double checked locking is a technique to preclude creating only about other illustration of Singleton when telephone outcry upward to getInstance() method is made inwards multi-threading environment. In Double checked locking pattern equally shown inwards below example, singleton illustration is checked 2 times before initialization. See here to larn to a greater extent than nigh double-checked-locking inwards Java. 

public static Singleton getInstance(){      if(_INSTANCE == null){          synchronized(Singleton.class){          //double checked locking - because 2d cheque of Singleton illustration amongst lock                 if(_INSTANCE == null){                     _INSTANCE = new Singleton();                 }             }          }      return _INSTANCE; }

Double checked locking should alone live on used when you lot have got requirement for lazy initialization otherwise use Enum to implement singleton or unproblematic static finally variable.



How do you lot preclude for creating only about other illustration of Singleton using clone() method?

This type of questions by together with large comes only about fourth dimension past times asking how to interruption singleton or when Singleton is non Singleton inwards Java.

Answer : Preferred agency is non to implement Cloneable interface equally why should i wants to do clone() of Singleton together with if you lot do only throw Exception from clone() method equally “Can non do clone of Singleton class”.



How do you lot preclude for creating only about other illustration of Singleton using reflection?

Open to all. In my persuasion throwing exception from constructor is an option.
Answer: This is similar to previous interview question. Since constructor of Singleton degree is supposed to live on somebody it prevents creating illustration of Singleton from exterior but Reflection tin shipping away access somebody fields together with methods, which opens a threat of only about other instance. This tin shipping away live on avoided past times throwing Exception from constructor equally “Singleton already initialized”



How do you lot preclude for creating only about other illustration of Singleton during serialization?

Another slap-up query which requires noesis of Serialization inwards Java and how to purpose it for persisting Singleton classes. This is opened upward to you lot all but inwards my persuasion purpose of readResolve() method tin shipping away sort this out for you.
Answer: You tin shipping away preclude this past times using readResolve() method, since during serialization readObject() is used to do illustration together with it render novel illustration every fourth dimension but past times using readResolve you lot tin shipping away supersede it amongst master Singleton instance. I have got shared code on how to do it inwards my postal service Enum equally Singleton inwards Java. This is too i of the argue I have got said that purpose Enum to do Singleton because serialization of enum is taken tending past times JVM together with it provides guaranteed of that.


When is Singleton non a Singleton inwards Java?

There is a really skilful article introduce inwards Sun's Java site which discusses diverse scenarios when a Singleton is non actually remains Singleton together with multiple illustration of Singleton is possible. Here is the link of that article http://java.sun.com/developer/technicalArticles/Programming/singletons/


Apart from these questions on Singleton pattern, only about of my reader contribute few to a greater extent than questions, which I included here. Thank you lot guys for your contribution.


Why you lot should avoid the singleton anti-pattern at all together with supersede it amongst DI?

Answer : Singleton Dependency Injection: every degree that needs access to a singleton gets the object through its constructors or amongst a DI-container.


Why Singleton is Anti pattern 

With to a greater extent than together with to a greater extent than classes calling getInstance() the code gets to a greater extent than together with to a greater extent than tightly coupled, monolithic, non testable together with difficult to modify together with difficult to reuse because of non configurable, hidden dependencies. Also, in that place would live on no demand for this clumsy double checked locking if you lot telephone outcry upward getInstance less oftentimes (i.e. once).


How many ways you lot tin shipping away write Singleton Class inwards Java?

Answer : I know at to the lowest degree 4 ways to implement Singleton pattern inwards Java
  1. Singleton past times synchronizing getInstance() method
  2. Singleton amongst world static finally land initialized during degree loading.
  3. Singleton generated past times static nested class, too referred equally Singleton holder pattern.
  4. From Java v on-wards using Enums


How to write thread-safe Singleton inwards Java?

Answer : Thread security Singleton commonly refers to write thread security code which creates i together with alone i illustration of Singleton if called past times multiple thread at same time. There are many ways to arrive at this similar past times using double checked locking technique equally shown inwards a higher house together with past times using Enum or Singleton initialized past times degree loader.

At finally few to a greater extent than questions for your practice, contributed past times Mansi, Thank you lot Mansi

14) Singleton vs Static Class?
15) When to select Singleton over Static Class?
16) Can you lot supersede Singleton amongst Static Class inwards Java?
17) Difference betwixt Singleton together with Static Class inwards java?
18) Advantage of Singleton over Static Class?


I have got covered answers of duet of these questions inwards my post, Singleton vs Static Class inwards Java - Pros together with Cons.  If you lot similar to read to a greater extent than Java interview questions you lot tin shipping away have got a await on only about of my favorites below

Further Learning
questions)
50+ Java Mulithreading Questions from Last three years (questions)
50+ Programmer Phone Interview Questions amongst answers (more)

Recommended Books
Java Programming Interview Exposed
Cracking the Coding Interview: 150 Programming Questions

0 Response to "10 Singleton Designing Interview Questions Inwards Coffee - Answered"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel