Design - Interview Questions and Answers for 'Pattern' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Rating

   
 Q31. What are the advantages and disadvantages of Factory Design Pattern ?Design
Ans. Object Oriented Design Patterns is the science of identifying the pattern to make objects communicate in a way to effectively implement a Solution.

Factory Design Patterns is the pattern that recommends creation of separate Factory Object for creation of other object. So its like saying - If you want to create an object of ClassA, Talk to FactoryObject ( which is an object of FactoryClass ). FactoryObject in itself encapsulates the inputs and logic required to make the decision regarding the creation and construction of object.

Advantage of Factory Pattern - Loose Coupling and Segregation of Responsibilities. Now instead of hard binding the complete logic to decide the nature and shape of the object on the basis of some conditions, you are assigning the responsibility to some other object and hence making the relationship loosely coupled and hence main tenable.

Disadvantages - Before Understanding the Dis-advantages , we should understand that these patterns were chosen after a period of evolution and research and almost best fit for the required solution, otherwise these patterns would have easily been replaced by now.

Though the advantages of these pattern surpass the disadvantages keeping in mind the decreasing cost of resources and increasing scale of applications, but still loose coupling by means of additional objects results in decreased performance.

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

   Like         Discuss         Correct / Improve     factory design pattern  design pattern


 Q32. Write a class explaining builder pattern ?Design
Ans.
public class BuggyBread { 

private String element1;

private String element2;

private BuggyBread(String element1, String element2){
this.element1 = element1;
this.element2 = element2;
}

public static class Builder {

private String element1;

private String element2;

Builder(BuggyBread buggybread){
element1 = buggybread.element1;
element2 = buggybread.element2;
}

Builder withElement1(String element1){
this.element1 = element1;
return this;
}

Builder withElement2(String element2){
this.element2 = element2;
return this;
}

BuggyBread build(){
BuggyBread buggybread = new BuggyBread(element1,element2);
return buggybread;
}
}
}


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

   Like         Discuss         Correct / Improve     builder pattern  builder class

Try 1 Question(s) Test


 Q33. Does it make sense to clone an object which is supposed to be immutable ?Design
Ans. It make sense only if we intend to modify either of the object and would like to preserve original state in other. Otherwise we can reuse the original object by making it singleton.

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

   Like         Discuss         Correct / Improve     clone  clone objects  prototype design pattern   immutable  immutability  immutability  cloning


 Q34. What are design situations to use Singleton and Prototype Design Pattern ?Design
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     Asked in 7 Companies      expert

Try 1 Question(s) Test


Frequently asked for Lead and Architect positions. Recently asked in many US companies.
  Q35. What are Anti Patterns ?Design
Ans. Anti-pattern is simply the creation of a pattern in your coding that negatively affects your code i.e the Negatives surpasses the positives.

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

   Like         Discuss         Correct / Improve     anti patterns  design patterns     Asked in 9 Companies        frequent


 Q36. What are the Wrapper classes available for primitive types ?Core Java
Ans. boolean - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void

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

   Like         Discuss         Correct / Improve     java   java5   data types   wrapper classes   adapter design pattern        rare


 Q37. What Design pattern Wrapper Classes implement ?Design
Ans. Adapter.

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

   Like         Discuss         Correct / Improve     java   wrapper classes   adapter design pattern   design pattern


 Q38. Difference between Factory and Abstract Factory Design Pattern ?Design
Ans. Factory Pattern deals with creation of objects delegated to a separate factory class whereas Abstract Factory patterns works around a super-factory which creates other factories.

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

   Like         Discuss         Correct / Improve     java   design pattern   factory design pattern   abstract factory design pattern   factory vs abstract factory      expert


 Q39. Difference between Factory and Builder Design Pattern ?Design
Ans. Builder pattern is the extension of Factory pattern wherein the Builder class builds a complex object in multiple steps.

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

   Like         Discuss         Correct / Improve     java   design pattern   factory design pattern   builder design pattern     Asked in 1 Companies      expert


 Q40. Difference between Proxy and Adapter ?Design
Ans. Adapter object has a different input than the real subject whereas Proxy object has the same input as the real subject. Proxy object is such that it should be placed as it is in place of the real subject.

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

   Like         Discuss         Correct / Improve     java   design pattern   proxy   adapter   proxy vs adapter      expert


 Q41. Difference between Adapter and Facade ?Design
Ans. The Difference between these patterns in only the intent. Adapter is used because the objects in current form cannot communicate where as in Facade , though the objects can communicate , A Facade object is placed between the client and subject to simplify the interface.

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

   Like         Discuss         Correct / Improve     java   design pattern   adapter   facade      expert


 Q42. Difference between Builder and Composite ?
Ans. Builder is a creational Design Pattern whereas Composite is a structural design pattern. Composite creates Parent - Child relations between your objects while Builder is used to create group of objects of predefined types.

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

   Like         Discuss         Correct / Improve     java   design pattern   builder   composite   creational design pattern   structural design pattern


 Q43. Example of Chain of Responsibility Design Pattern ?
Ans. Exception Handling Throw mechanism.

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

   Like         Discuss         Correct / Improve     java   design pattern   chain of responsibility      expert


 Q44. Example of Observer Design Pattern ?
Ans. Listeners.

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

   Like         Discuss         Correct / Improve     java   design pattern   listeners   observer design pattern      expert


 Q45. Difference between Factory and Strategy Design Pattern ?Design
Ans. Factory is a creational design pattern whereas Strategy is behavioral design pattern. Factory revolves around the creation of object at runtime whereas Strategy or Policy revolves around the decision at runtime.

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

   Like         Discuss         Correct / Improve     java   design pattern   factory   strategy   creational design pattern   behavioral design pattern     Asked in 1 Companies      expert


 Q46. Shall we use abstract classes or Interfaces in Policy / Strategy Design Pattern ?
Ans. Strategy deals only with decision making at runtime so Interfaces should be used.

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

   Like         Discuss         Correct / Improve     java   design pattern   strategy design patterm      expert


Very frequently asked.Usually among first few questions.
  Q47. What is MVC ? Design
Ans. MVC is a Design Pattern that facilititates loose coupling by segregating responsibilities in a Web application

1. Controller receives the requests and handles overall control of the request
2. Model holds majority of the Business logic, and
3. View comprise of the view objects and GUI component

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

   Like         Discuss         Correct / Improve     j2ee   mvc   mvc design pattern   design pattern   struts   spring   web application   web frameworks   ebay     Asked in 60 Companies      basic        frequent

Try 1 Question(s) Test


 Q48. What's wrong with this code ?

public static void main(String[] args) {
String regex = "(\\w+)*";
String s = "Java is a programming language.";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while (matcher.next()) {
System.out.println("The e-mail id is: " + matcher.group());
}
}
Core Java
Ans. matcher.find() should have been used instead of matcher.next() within while.

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

   Like         Discuss         Correct / Improve     java   regex   pattern.matcher   java.util   coding   code


 Q49. Which methods of the Pattern class have equivalent methods in the String class? Core Java
Ans. split() and macthes()

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

   Like         Discuss         Correct / Improve     java   io   string class   string   pattern


Very frequently asked. Usually followed by questions related to private constructor and synchronized access. Frequently asked in JPMorgan and TCS (Based on 2 feedback)
  Q50. Explain Singleton Design Pattern ?Design
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     Asked in 46 Companies      intermediate        frequent


 Q51. Give an Example for Builder Pattern ?Core Java
Ans. String str = new StringBuilder().append("Buggy").append("Bread").toString();

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

   Like         Discuss         Correct / Improve     java   builder pattern   design pattern   stringbuilder   string     Asked in 3 Companies

Try 1 Question(s) Test


 Q52. 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.
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


Almost sure to be asked in every company using any Dependency Injection framework ( Spring, Guice etc )
  Q53. What is Dependency Injection or IOC ( Inversion of Control ) ?Design
Ans. It is a Design Pattern that facilitates loose coupling by sending the dependency information ( object references of dependent object ) while building the state of the object. Objects are designed in a manner where they receive instances of the objects from other pieces of code, instead of constructing them internally and hence provide better flexibility.

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

   Like         Discuss         Correct / Improve     design patterns   ioc ( Inversion of Control )  dependency injection     Asked in 83 Companies      intermediate        frequent


 Q54. 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     Asked in 1 Companies      Intermediate        frequent


 Q55. Write code for the usage of Builder Design Pattern
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?&category=code&searchOption&keyword=964

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

   Like         Discuss         Correct / Improve     builder design pattern  builder pattern  code  coding      intermediate


 Q56. With the following code, Which is a valid way to initialize ?
public class BuggyBread {

   private String element1;

   private String element2;

   private BuggyBread(String element1, String element2){
      this.element1 = element1;
      this.element2 = element2;
   }

   public static class Builder {
   
      private String element1;

      private String element2;

      Builder(BuggyBread buggybread){
         element1 = buggybread.element1;
         element2 = buggybread.element2;
      }

      Builder withElement1(String element1){
         this.element1 = element1;
         return this;
      }

      Builder withElement2(String element2){
         this.element2 = element2;
         return this;
      }

      BuggyBread build(){
         BuggyBread buggybread = new BuggyBread(element1,element2);
         return buggybread;
      }
   }
}
Core Java
a. BuggyBread buggybread = new BuggyBread();
b. BuggyBread buggybread = new BuggyBread("element1","element2");
c. BuggyBread.Builder builder = new BuggyBread.Builder();
d. BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");

Ans.d. BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");

 Q57. Listeners are example of ..Design
a. Factory design Pattern
b. Abstract Factory Design Pattern
c. Singleton Design Pattern
d. Observer Design Pattern

Ans.d. Observer Design Pattern

 Q58. Which of the following Design pattern has been used with Hibernate Criteria ?Hibernate
a. Filter
b. Prototype
c. Builder
d. Observer

Ans.a. Filter

previous 30   

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: