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.

How are design patterns different from algorithms?

Answer»

Both Design Patterns and ALGORITHMS describe TYPICAL solutions to any GIVEN problem. But the main difference is that the algorithm defines a clear set of actions for achieving a GOAL and a design pattern provides a high-level description of any solution. Design patterns applied to TWO different problems might be the same but the logic of implementation would be different and is based on the requirements.

2.

How are design principles different from design patterns?

Answer»
  • Design PRINCIPLES are those principles that are followed while designing software systems for any platform by making USE of any programming language. SOLID principles are the design principles that we follow as guidelines to develop robust, extensible and scalable software systems. These APPLY to all aspects of programming.
  • Design Patterns are the reusable template solutions for commonly occurring problems that can be customized as per the problem requirements. These are well-implemented solutions that are tested PROPERLY and are safe to use. Factory Design Pattern, Singleton pattern, Strategy patterns are a few of the examples of design patterns.
3.

What are some of the design patterns used in Java’s JDK library?

Answer»

FOLLOWING are some DESIGN patterns used in Java’s JDK library:

  • Decorator pattern are used by the Wrapper classes.
  • Singleton pattern is used in classes like CALENDAR and Runtime.
  • Factory pattern is used for methods like Integer.valueOf methods in wrapper classes.
  • Observer pattern is used for handling event frameworks like AWT, swing etc.
4.

What do you understand by the Open-Closed Principle (OCP)?

Answer»

The Open close principle states that any class, component or entity should be open for extension but closed for modification. A class can be extended via Inheritance, Interfaces, COMPOSITION whenever required instead of modifying the code of the class. Consider an INSTANCE where we have a class that calculates the area of a square. Later, we GET the requirement of calculating the area of a rectangle. Here, instead of modifying the ORIGINAL class, we can create one base class and this base class can be extended by the NEW class rectangle.

5.

What are the SOLID Principles?

Answer»

SOLID PRINCIPLES were the Object-Oriented principles introduced by Robert C. Martin in his paperwork “Design Principles and Design patterns” in the year 2000. The acronym for SOLID goes as follows:

  • S - Single Responsibility Principle (SRP): The single responsibility principle ensures that every CLASS or module should be accountable and responsible for only one functionality. There should be one and only one reason for changing any class.
  • O - Open Closed Principle (OCP): Every class is open for extension but closed for modification. Here, we are allowed to extend the entities behaviour by not modifying anything in the existing source code.
  • L - Liskov Substitution Principle(LSP): LSP principle states that the objects can be replaced by the subtype instances without affecting the correctness of the program.
  • I - Interface Segregation Principle (ISP): The ISP principle states that we can use as many interfaces specific to the CLIENT’s requirements instead of creating only one general interface. CLIENTS should not be forced to implement the functionalities that they do not require.
  • D - Dependency Inversion Principle: Here, the high-level modules should not be DEPENDENT on the lower level modules or concrete implementations. Instead, they should be dependent on the abstractions.
6.

What Is Gang of Four (GOF) in Design Patterns?

Answer»

Gang of Four (GOF) are the 4 superheroes who invented the concept of design patterns. These heroes are ERICH Gamma, Ralph JOHNSON, Richard HEL and John Vlissides. These people documented the design patterns in a book called “Design Patterns: ELEMENTS of Reusable Object-Oriented Software” in 1995. If not for these people, the software developers WOULD have wasted time-solving problems of recurring nature instead of focussing on business requirements.

7.

What is Inversion of Control?

Answer»

Inversion of control is a pattern used to decouple the dependencies between layers and components in the system. The DEPENDENCY-Injection (DI) pattern is an EXAMPLE of an IoC pattern that helps in removing dependencies in the code.

Let us understand this with the help of an example. CONSIDER we have a class A that makes use of class B as shown below:

public class A{ private B b; public A(){ this.b = new B(); }}

Here, we have a dependency between classes A and B. If we had the IoC pattern implemented, we would not have used the new operator to assign value to the dependent variable. It would have been something as shown below:

public class A { private IocB b; public A(IocB b) { this.b = b; }}

We have inverted the control of HANDING the dependency of instantiating the object of class B to the IoC class IocB.

8.

What are the types of design patterns in Java?

Answer»

There are three types of design patterns. They are:

  • Creational Patterns: These patterns provide freedom of CHOICE between creating objects by hiding the logic. The objects constructed are decoupled from the implemented SYSTEM. Some of the examples of creational patterns are - Factory design pattern, Builder design, Prototype design, Singleton design, Abstract Factory design.
  • Structural Patterns: These patterns help in defining how the structures of classes and objects should be like for defining the composition between classes, interfaces and objects. Some of the examples of structural patterns are - Adaptor design, Facade design, DECORATOR design, proxy design etc.
  • Behavioural Patterns: These patterns help to define how the objects should COMMUNICATE and interact with one another. Some of the examples of behavioural patterns are - Command pattern, Iterator pattern, Observer pattern, Strategy pattern, etc.

The following diagram represents the summary of the types of design patterns.

9.

How can you describe a design pattern?

Answer»

For describing a design PATTERN, we follow the below things:

  • Define a pattern name and what classification of design pattern the pattern would fall to.
  • Define a PROBLEM and what is the corresponding solution
  • What are the variations and language-dependent ALTERNATIVES for the problem that NEEDS to be addressed?
  • What are the real-time use cases and the efficiency of the software that uses these patterns?
10.

What are the advantages of Java Design Patterns?

Answer»

Design patterns are TEMPLATE-based reusable solutions to help developers work EFFORTLESSLY in multiple projects. In Java, the design patterns are flexible and help to identify unwanted repetitive code easily. The architecture of the SOFTWARE can be customised as per the requirements. Some of the advantages of using design patterns in Java are:

  • They are reusable and can be used in multiple projects.
  • They provide template solutions for DEFINING system architecture.
  • They provide TRANSPARENCY to software design.
  • They are well-tested and proven means of developing robust solutions effortlessly.
11.

What are design patterns?

Answer»

Design PATTERNS are the reusable solutions that solve common problems of software development. These problems include repetitive code, redundant functions and logic etc. These help to save considerable EFFORT and time required for the developers while developing software. Design patterns are commonly USED in object-oriented software products by incorporating best practices and PROMOTING REUSABILITY for developing robust code.