Interview Questions
« Previous | 0 | 1 | 2 | 3 | 4 | Next »

1.What are Design Patterns?

  • Design patterns represent the best practices used by experienced object-oriented software developers.

  • Design patterns are solutions to general problems that software developers faced during software development.

  • These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.

2.What is Gang of Four GOF?

  • In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development.

  • These authors are collectively known as Gang of Four GOF.

3.Name types of Design Patterns?

Design patterns can be classified in three categories: Creational, Structural and Behavioral patterns.

  • Creational Patterns - These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case.

  • Structural Patterns - These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.

  • Behavioral Patterns - These design patterns are specifically concerned with communication between objects.

4.What are J2EE Patterns?

  • These design patterns are specifically concerned with the presentation tier.

  • These patterns are identified by Sun Java Center.

5.What is Factory pattern?

  • Factory pattern is one of most used design pattern in Java.

  • Thistype of design pattern comesunder creational pattern as this pattern provides one of the best ways to create an object.

  • In Factory pattern, we create object without exposing the creation logic to the client and refer tonewly created object using a common interface.

6.What is Abstract Factory pattern?

  • Abstract Factory patterns work around a super-factory which creates other factories.

  • This factory isalso called as factory of factories.

  • This type of design pattern comes under creational pattern asthis pattern provides one of the best ways to create an object.

  • In Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes.

  • Each generated factory can give the objects as per the Factory pattern.

7.What is Singleton pattern?

  • Singleton pattern is one of the simplest design patterns in Java.

  • This type of design pattern comesunder creational pattern as this pattern provides one of the best ways to create an object.

  • This pattern involves a single class which is responsible to create an object while making sure that only single object gets created.

  • This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

8.How can you create Singleton class in java?

  • It is two step process.

  • First, make the constructor private so that new operator cannot be used to instantiate the class.

  • Return an object of the object if not null otherwise create the object and return the same via a method.

9.What are the difference between a static class and a singleton class?

Following are the differences between a static class and a singleton class.

  • A static class can not be a top level class and can not implement interfaces where a singleton class can.

  • All members of a static class are static but for a Singleton class it is not a requirement.

  • A static class get initialized when it is loaded so it can not be lazily loaded where a singleton class can be lazily loaded.

  • A static class object is stored in stack whereas singlton class object is stored in heap memory space.

10.Can we create a clone of a singleton object?

  • Yes.

11.How to prevent cloning of a singleton object?

  • Throw exception within the body of clone method.

12.Name some of the design patterns which are used in JDK library?

Following are some of the design patterns which are used in JDK library.

  • Decorator patttern is used by Wrapper classes.

  • Singleton pattern is used by Runtime, Calendar classes.

  • Factory pattern is used by Wrapper class like Integer.valueOf.

  • Observer pattern is used by event handling frameworks like swing, awt.

13.What is the benefit of Factory pattern?

  • Factory pattern encapsulates the implementation details and underlying implementation can be changed without any impact on caller api.

14.What is Builder pattern?

  • Builder pattern builds a complex object using simple objects and using a step by step approach.

  • This builder is independent of other objects.

15.What is Prototype pattern?

  • Prototype pattern refers to creating duplicate object while keeping performance in mind.

  • This pattern involves implementing a prototype interface which tells to create a clone of the current object.

16.When Prototype pattern is to be used?

  • This pattern is used when creation of object directly is costly.

  • For example, an object is to be created after a costly database operation.

  • We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.

17.What is Adapter pattern?

  • Adapter pattern works as a bridge between two incompatible interfaces.

  • This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces.

  • Give an example of Adapter pattern.

  • A real life example could be a case of card reader which acts as an adapter between memory card and a laptop.

  • You plugin the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.

18.What is Bridge pattern?

  • Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently.

  • This type of design pattern comes under structural pattern as this pattern decouples implementation class and abstract class by providing a bridge structure between them.

  • This pattern involves an interface which acts as a bridge which makes the functionality of concrete classes independent from interface implementer classes.

  • Both types of classes can be altered structurally without affecting each other.

19.What is Filter pattern?

  • Filter pattern or Criteria pattern is a design pattern that enables developers to filter a set of objects using different criteria and chaining them in a decoupled way through logical operations.

  • This type of design pattern comes under structural pattern as this pattern combines multiple criteria to obtain single criteria.

20.What is Composite pattern?

  • Composite pattern is used where we need to treat a group of objects in similar way as a single object.

  • Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy.

  • This type of design pattern comes under structural pattern as this pattern creates a tree structure of group of objects.

  • This pattern creates a class that contains group of its own objects.

  • This class provides ways to modify its group of same objects.

« Previous | 0 | 1 | 2 | 3 | 4 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com