Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What Is Observer Design Pattern In Java? When Do You Use Observer Pattern In Java?

Answer»

Observer PATTERN is BASED upon notification, there are TWO kinds of object Subject and Observer.Whenever there is change on subject's STATE observer will receive notification.

Observer pattern is based upon notification, there are two kinds of object Subject and Observer.Whenever there is change on subject's state observer will receive notification.

2.

What Is Factory Pattern In Java? What Is Advantage Of Using Static Factory Method To Create Object?

Answer»

Factory pattern used to create OBJECT by providing static factory methods. There are MANY ADVANTAGE of providing factory methods e.g. caching immutable OBJECTS, easy to introduce new objects etc.

Factory pattern used to create object by providing static factory methods. There are many advantage of providing factory methods e.g. caching immutable objects, easy to introduce new objects etc.

3.

Can You Write Thread-safe Singleton In Java?

Answer»

There are multiple ways to write thread-safe singleton in Java e.g by writing singleton using double checked LOCKING, by using STATIC Singleton instance initialized during class loading. By the way using Java enum to CREATE thread-safe singleton is most SIMPLE way.

There are multiple ways to write thread-safe singleton in Java e.g by writing singleton using double checked locking, by using static Singleton instance initialized during class loading. By the way using Java enum to create thread-safe singleton is most simple way.

4.

What Is Singleton Pattern In Java?

Answer»

Singleton pattern in Java is a pattern which ALLOWS only one instance of Singleton CLASS AVAILABLE in WHOLE application.

Singleton pattern in Java is a pattern which allows only one instance of Singleton class available in whole application.

5.

When To Use Composite Design Pattern In Java? Have You Used Previously In Your Project?

Answer»
  • This design pattern question is asked on Java interview not just to check FAMILIARITY with Composite pattern but also, whether candidate has real life experience or not.
  • Composite pattern is also a core Java design pattern, which allows you to treat both whole and part OBJECT to treat in SIMILAR way.
  • Client code, which deals with Composite or individual object doesn't differentiate on them, it is POSSIBLE because Composite class also implement same interface as there individual part.
  • One of the good example of Composite pattern from JDK is JPanel class, which is both Component and Container.
  • When paint() method is called on JPanel, it internally called paint() method of individual components and LET them draw themselves.
  • On second part of this design pattern interview question, be truthful, if you have used then say yes, otherwise say that you are familiar with concept and used it by your own.
  • By the way always remember, giving an example from your project creates better impression.

6.

When Will You Use A Factory Pattern?

Answer»

The factory pattern is PREFERRED in the following cases: - a class does not know which class of objects it must CREATE - factory pattern can be used where we need to create an object of any one of sub-classes DEPENDING on the DATA provided

The factory pattern is preferred in the following cases: - a class does not know which class of objects it must create - factory pattern can be used where we need to create an object of any one of sub-classes depending on the data provided

7.

Define A Factory With Respect To Software Development?

Answer»
  • Object construction is referred as a factory.
  • It is used to isolate the creation of OBJECTS from their usage.
  • If you are using factory METHODS for objects creation, then the new derived types will cause no CODE change in your CLASSES that are using factories.

8.

Which Object Oriented Method Is Used By The Creational Patterns To Instantiate Object?

Answer»

CREATIONAL PATTERNS use inheritance to decide the object to be INSTANTIATED.

Creational patterns use inheritance to decide the object to be instantiated.

9.

A Class Instance Can Be Created Using New Operator. Why Should We Use Creational Design Patterns To Create Objects?

Answer»
  • Using new OPERATOR to create objects is a valid APPROACH but it's like hard CODING the object type.
  • If we are 100% sure that our object will be of the same class all the TIME, then we use new operator to create an object.
  • In scenarios where the NATURE of the object can change according to the nature of the program, we use creational design patterns which offer flexible approach for creating class instances.

10.

Define Creational Design Patterns?

Answer»
  • Creational patterns are USED to DEFINE and DESCRIBE how objects are created at class instantiation time.
  • Usually an abstract super class contains the details of the CLASSES that are INSTANTIATED and the client class is unaware of such details.
  • Singletion pattern, factory method pattern, abstract factory pattern are examples of creational design pattern.

11.

Give Example Of Decorator Design Pattern In Java ? Does It Operate On Object Level Or Class Level?

Answer»
  • DECORATOR pattern ENHANCES capability of individual OBJECT.
  • Java IO uses decorator pattern extensively and classical example is Buffered classes like BufferedReader and BufferedWriter which enhances READER and WRITER objects to perform Buffer level reading and writing for improved performance.

12.

What Is Observer Design Pattern In Java?

Answer»
  • OBSERVER design pattern is based on communicating changes in state of OBJECT to OBSERVERS so that they can take there ACTION.
  • Simple example is a weather system where change in weather MUST be reflected in Views to show to public.
  • Here weather object is Subject while different views are Observers.
  • Look on this article for complete example of Observer pattern in Java.

13.

What Is Data Access Object Patterndao Pattern?

Answer»

Data Access Object Pattern or DAO pattern is used to separate LOW level data accessing API or operations from high level business services.

Following are the participants in Data Access Object Pattern:

  • Data Access Object Interface - This interface defines the standard operations to be performed on a MODEL objects.
  • Data Access Object concrete class - This class implements above interface. This class is RESPONSIBLE to get data from a data SOURCE which can be database / xml or any other storage mechanism.
  • Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.

Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services.

Following are the participants in Data Access Object Pattern:

14.

What Is Gang Of Four Gof?

Answer»

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.

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.