Java - Interview Questions and Answers for 'Singleton' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Singleton' - 13 question(s) found - Order By Newest

 Q1. Difference between Static and Singleton Class ?Core Java2016-11-30 21:00:43

Ans. 1. Static class is a class which cannot be instantiated and all its members are static whereas Singleton is the class that only permit creation of single object and then the object is reused.

2. As there is no object in Static class, it cannot participate in runtime Polymorphism.

3. As Static class doesnt allow creating objects and hence it cannot be serialized.

4. Static class body is initialized eagerly at application load time whereas Singleton object can be initiated eagerly using static blocks or lazily on first need.

5. Its not recommended to use pure static class as it fails to use many OOPs concepts.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     Static Class  Singleton  Static Class vs Singleton     Netflix  EPAM  Ebay      Intermediate        frequent


 Q2. What is the difference between these two approaches of creating singleton Class ?

//Double Checked Locking Code
public static Singleton createInstance() {
   if(singleton == null){
      synchronized(Singleton.class) {
         if(singleton == null) {
            singleton = new Singleton();
         }
      }
   }
   return singleton;
}

//Single checked locking code
public static Singleton createInstance() {
   synchronized(Singleton.class) {
      if(singleton == null) {
         singleton = new Singleton();
      }
   }
   return singleton;
}
Design
Admin
info@buggybread.com
Ans. In First Case , Lock for the synchronized block will be received only if singleton == null whereas in second case every thread will acquire the lock before executing the code.
The problem of synchronization with singleton will only happen when the object has not be instantiated. Once instantiated , the check singleton == null will always generate true and the same object will be returned and hence no problem. First condition will make sure that synchronized access ( acquiring locks ) will only take place if the object has not been created so far.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   singleton   synchronization

Try 1 Question(s) Test


Very frequently asked. Usually followed by questions related to private constructor and synchronized access. Frequently asked in JPMorgan and TCS (Based on 2 feedback)
  Q3. Explain Singleton Design Pattern ?Design
Admin
info@buggybread.com
Ans. http://www.buggybread.com/2014/03/java-design-pattern-singleton-interview.html

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   design pattern   singleton   at&t   ebay  fidelity india  united healthcare india     JP Morgan  Tata Consultancy (TCS)  HCL Technologies  Wipro  Infosys  Tibco  Edline  JPMorgan Chase  Sabre  Bank Of America India  Fidelity  Samsung India  Robert Bosch India  ZapCom  Goldman Sachs India  Eze Software  ZapCom  Dailymotion  Netflix  CenturyLink  Misfit  TSYS  ECI Telecom  Dealogic  Robert Bosch  Redolent  EFI  CSX  Prolific  Validus  Birlasoft  AdNovum  NCR  Ray Business Technologies  Nagravision  Unify  SLK Group  DBS Bank   Razorthink  GST Global  USAA  Ray Technologies  Ray Business Technologies (Raybiztech)  Raybiztech      intermediate        frequent


 Q4. Shouldn't we make a class with all static members is its just expected to be executed as a standalone program with just one thread. Moreover Lets assume that there is no runtime Polymorphism required and there is no need for serialization ?Design
Admin
info@buggybread.com
Ans. Still No in case we are making use of inheritance. we may have problem wherein we have program flow moving across common inherited method and specific methods of the derived class. call made to another static method in the parent class will only access the static class of the Parent class irrespective of the call from any of the derived class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     static class   static vs singleton   inheritance


 Q5. Should we have instance variables in the Spring Bean which has been scoped as Singleton ?Spring
Admin
info@buggybread.com
Ans. No, if required we should only have final variables.Bean scoped singleton means that only one instance of the bean will be created and will be shared among different requests and hence instance variables will get shared too.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   singleton   spring framework   spring beans


 Q6. What are stateless objects ? How are they different from immutable objects ? Which of these two is thread safe ?
Admin
info@buggybread.com
Ans. Stateless objects are the objects without instance fields (instance variables). The class may have compile time constants i.e static final fields.

Immutable objects are the objects which have state but the state cannot be changed after initialization.

Both are Thread safe.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   stateless objects   immutable objects   objects   singleton scope


 Q7. Which of the following is not the difference between Singleton and Static class ( Class with static members only ) ?

a. Only one object can be created for Singleton class whereas No objects are created for static class.
b. Singleton class instance is initiated using new keyword whereas static class instance is created using static method.
c. Singleton class can be serialized whereas Static class cannot be.
d. Singleton Class can participate in runtime Polymorphism whereas Static class cannot.
Anonymous
Ans. Singleton class instance is initiated using new keyword whereas static class instance is created using static method.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   singleton   design pattern   static class


 Q8. Which of the following do you think is the primary reason you would never use a static class even the application doesn't need multiple requests or threads ?

a. Serialization
b. Runtime Polymorphism
c. Lazy Loading
d. Memory
Anonymous
Ans. Runtime Polymorphism

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     static class   static vs singleton   java   oops   objects  Runtime Polymorphism


 Q9. Write code for singleton classDesign

Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=singleton+class&category=code

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     singleton  code  coding  design pattern     ECI Telecom      Intermediate        frequent



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. What are design situations to use Singleton and Prototype Design Pattern ?Design2016-07-28 20:28:43

Ans. Both are creational design patterns.

Singleton is used when we would like to reuse an object if object is not supposed to hold request or thread specific information. Inversely Prototype is used in situations where we would like to reuse the object information but the request / thread may require it own data to be persisted.

In short, Singleton is used in situations where we can live with single object being shared across multiple requests or threads whereas Prototype is used when we need duplicate copies of objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     design pattern  singleton  prototype  creational design pattrn     Mphasis  JP Morgan  Fidelity  EPAM  Airline Tariff  Vimukti Technologies  Cardinal Health      expert

Try 1 Question(s) Test


 Q11. How to make sure that only one instance is created in Singleton Pattern ?Core Java2016-12-21 10:00:04

Ans. By Using Double Checked Locking code

//Double Checked Locking Code
public static Singleton createInstance() {
if(singleton == null){
synchronized(Singleton.class) {
if(singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     singleton  design pattern     Bank Of America India


 Q12. Difference between Singleton and Factory Design Pattern ?Design2017-02-02 11:24:01

Ans. Both are creational design patterns but singleton facilitates in creation and reuse of single object whereas Factory deals with creation of multiple objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     Design pattern  singleton  factory  singleton vs factory     Jabong.com  Intuit  Kumaran Systems  BirlaSoft


 Q13. What is the difference between Singleton and Prototype Bean scope in Spring ?Spring2017-08-20 06:35:16

Ans. Prototype scope - A new object is created each time it is injected/looked up. It will use new SomeClass() each time.

Singleton scope - It is the default scope. The same object is returned each time it is injected/looked up. Here it will instantiate one instance of SomeClass and then return it each time.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     Spring beans  singleton bean scope  prototype bean scope     Infosys Technologies




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: