Java Design Patterns Interview Questions with Answers:-
1. Define what is Java Design Pattern?
A design pattern is a language-independent strategies for solving the common object-oriented design problem. It describes how to structure classes to meet a given requirement.
2. Define what is creational design patterns and Factory pattern?
Creational design pattern: This pattern is used to define and describe how objects are created at class instantiation time.
Factory pattern: The factory pattern is used to create an object without exposing the creation logic to the client and refer to a newly created object using a common interface.
3. Which design pattern is used to get a way to access the elements of a collection object in a sequential manner?
Iterator pattern is used to get a way to access the elements of a collection object in a sequential manner.
4. When service locator pattern is used?
When we want to locate various services using JNDI we use service locator pattern.
5. Explain in how many ways can you create singleton pattern?
To create single objects there are two famous ways
- Lazy loading
- Eager loading
6. Define which pattern is used when we need to decouple an abstraction from its implementation?
When we want to decouple an abstraction from its implementation in order that two can vary independently we use the bridge pattern.
7. Define which design pattern will be helpful to add new functionality to an existing object?
A decorator pattern allows a user to add new functionality to an existing object without changing its structure.
8. How can you create a 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. Is it possible to write thread-safe singleton in Java?
To write thread-safe singleton in Java there are multiple ways for example by using static singleton instance initialized during class loading, by writing singleton using double-checked locking. Java Enum is the simplest way to create thread-safe singleton.
10. How one should describe a design pattern?
To describe a design pattern, the following things need to be taken care of
- Pattern name and classification
- Problem and solution
- Consequences: Variation and language dependent alternatives should also be addressed
- Know Uses: Identify the uses in the real systems and its efficiency
11. Define why access to the non-static variable is not allowed from the static method in Java?
You cannot access non-static data from static context because the non-static variable is associated with a specific instance of an object while static is not associated with any instance.
12. Define which pattern is useful when one has to pass data with multiple attributes in one shot from client to server?
Transfer Object Pattern is useful when one has to pass data with multiple attributes in one shot from the client to the server.
13. Name some of the entities of DAO pattern?
Some of the entities of DAO include,
- Data access object concrete class
- Data access object interface
- Model object or value object
14. Define when can you use the Intercepting pattern?
Intercepting pattern is used when you have to do some pre-processing or post-processing with request or response of the application.
15. Define when to use a Factory Pattern?
Factory pattern can be used,
- When a class does not know which class of objects needs to create
- When class specifies its sub-classes to specify which objects to create
- In a programming language, you can use factory pattern where you have to create an object of any one of sub-classes depending on the given data
JAVA DESIGN PATTERNS Questions pdf free download::
16. Explain in singleton pattern whether it is better to make the whole getinstance() method synchronized or just critical section is enough? Which one is preferable?
Synchronization of whole get Instance() method is costly and is only needed during the initialization on singleton instance, to stop creating another instance of Singleton. Therefore it is better to only synchronize critical section and not the whole method.
17. Explain in how many ways can you write singleton class in Java?
One can write singleton class in Java in four ways
- Singleton with public static final field initialized during class loading
- Singleton generated by the static nested class also referred to as singleton holder pattern
- Singleton by synchronizing get Instance () method
- From Java 5 on-wards using Enums
18. How can you prevent creating another instance of Singleton using clone() method?
The preferred way to prevent creating another instance of a singleton is by not implementing Cloneable interface and if you do just throw an exception from clone() method “ not to create a clone of singleton class”.
19. What is the difference between “throw” and “throws”?
Keyword “Throw” is used to explicitly throw as an exception, while “Throws” is utilized to handle checked exceptions for re-intimating the compiler that exceptions are being handled. The throws need to be used in the method’s definition and also while invoking the method that raises checked exceptions.
20. Define which classes in JDK uses the singleton pattern?
Java.lang.Runtime classes use a singleton pattern in JDK.
21. Define what is the limitation of using a singleton pattern?
The singleton pattern ensures that a class has only one instance and to provide a global point of access to it. But at the same time, this becomes its limitation as most classes in an application you will need to create multiple instances.
22. Define what is the difference between VO and JDO?
The difference between JDO and VO is that the JDO is a persistent technology that competes against entity beans in enterprise application development. It enables you to create POJO (plain old java objects) and persist them to the database.
While VO stands for value objects represents an abstract design pattern used in conjunction with entity beans, JDBC and possibly even JDO to overcome commonly found isolation and transactional problems in enterprise apps.
23. Define what is Singleton pattern in Java?
Singleton pattern in Java is a pattern which allows only one instance of Singleton class available in the whole application. java.lang.Runtime is a good example of a Singleton pattern in Java. There are lot’s of follow up questions on Singleton pattern see 10 Java singleton interview question answers for those follow-ups
24. Can you write thread-safe Singleton in Java?
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 the most simple way. See Why Enum singleton is better in Java for more details.
25. When to use the Template method design Pattern in Java?
Template pattern is another popular core Java design pattern interview question. I have seen it appear many times in real life project itself. Template pattern outlines an algorithm in the form of a template method and lets subclass implement individual steps. A key point to mention, while answering this question is that template method should be final, so that subclass cannot override and change steps of an algorithm, but same time individual step should be abstract, so that child classes can implement them.
26. Define what is Factory pattern in Java? What is the advantage of using a static factory method to create an object?
Factory pattern in Java is a creation Java design pattern and favorite on many Java interviews. Factory pattern used to create an object by providing static factory methods. There are many advantages of providing factory methods e.g. caching immutable objects, easy to introduce new objects, etc. See What is Factory pattern in Java and benefits for more details.
27. What is the Builder design pattern in Java? When do you use the Builder pattern?
Builder pattern in Java is another creational design pattern in Java and often asked in Java interviews because of its specific use when you need to build an object which requires multiple properties some optional and some mandatory. See When to use Builder pattern in Java for more details
28. Can you give an example of SOLID design principles in Java?
There are lots of SOLID design pattern which forms acronym SOLID, read this list of SOLID design principles for Java programmer to answer this Java interview question.
29. Define what is the difference between Abstraction and Encapsulation in Java?
I have already covered the answer of this Java interview question in my previous post as the Difference between encapsulation and abstraction in Java. See there to answer this question.