Join us
@dridhone ă» Jul 19,2023 ă» 5 min read ă» 718 views
Top 22 Java Design Patterns Interview Questions & Answers (2021)
1) Explain what is Java Design Pattern?
A design pattern is a language independent strategy for solving common object-oriented design problem. It describes how to structure classes to meet a given requirement.
2) Explain 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 sequential manner?
Iterator pattern is used to get a way to access the elements of a collection object in sequential manner.
4) When service locator pattern is used?
When we want to locate various services using JNDI we use service locator pattern.
5) Mention in how many ways can you create singleton pattern?
To create single objects there are two famous ways
6) Mention 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 bridge pattern.
7) Mention 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) Explain how can you create a Singleton class in Java?
It is two step process,
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) Mention how one should describe a design pattern?
To describe a design pattern, following things need to be taken care of
11) Mention why access to the non-static variable is not allowed from static method in Java?
You cannot access non-static data from static context because non-static variable are associated with a specific instance of an object while static is not associated with any instance.
12) Mention 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 client to the server.
13) What is not Machine Learning?
Some of the entities of DAO include,
14) Mention when can you use the Intercepting pattern?
15) Mention when to use a Factory Pattern?
Factory pattern can be used,
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 getinstance() 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) Mention in how many ways can you write singleton class in Java?
One can write singleton class in Java in four ways
18) Explain 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) Mention 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) Mention which classes in JDK uses singleton pattern?
Java.lang.Runtime classes uses singleton pattern in JDK.
21) Mention what is the limitation of using 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) Mention what is the difference between VO and JDO?
The difference between JDO and VO is that the JDO is a persistent technology that compete 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 conjuction with entity beans, jdbc and possibly even JDO to overcome commonly found isolation and transactional problems in enterprise apps.
Join other developers and claim your FAUN account now!
Founder, www.dridhon.com
@dridhoneInfluence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.