0% found this document useful (0 votes)
539 views

Java/J2EE Design Patterns Interview Questions You'll Most Likely Be Asked: Second Edition

Java/J2EE Design Patterns Interview Questions You’ll Most Likely Be Asked is a perfect companion to stand ahead above the rest in today’s competitive job market. Rather than going through comprehensive, textbook-sized reference guides, this book includes only the information required immediately for job search to build an IT career. This book puts the interviewee in the driver’s seat and helps them steer their way to impress the interviewer.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
539 views

Java/J2EE Design Patterns Interview Questions You'll Most Likely Be Asked: Second Edition

Java/J2EE Design Patterns Interview Questions You’ll Most Likely Be Asked is a perfect companion to stand ahead above the rest in today’s competitive job market. Rather than going through comprehensive, textbook-sized reference guides, this book includes only the information required immediately for job search to build an IT career. This book puts the interviewee in the driver’s seat and helps them steer their way to impress the interviewer.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Java/J2EE Design

Patterns Interview
Questions
Review these typical interview questions and think about how you would
answer them. Read the answers listed; you will find best possible answers
along with strategies and suggestions.
This page is intentionally left blank
Chapter 1

Introduction to Design

1: Give some examples of design patterns used within JDK.


Answer:
a) Adapter pattern – This is useful to convert one type of object
to another type of object. So basically, you can make an
incompatible object, compatible with another class. A good
example of this is the Arrays.asList method. This method in
the java.util.Arrays class is used to convert an array into a
java.util.List.
b) Decorator Pattern – This is used to add additional behavior
to an object. The Collections.synchronisedList is an example.
It accepts a List object and returns a synchronised version of
the List. So, it adds the additional functionality of
synchronising the List. Similarly, the Collections
unmodifiable Map is an example since it accepts
a Map object and returns a Map which is read-only.
c) Builder pattern – This is used to move the object creation
into a separate process. This helps to construct complex
objects via easy steps. Not only that, once you define the
creation process, you can use this to build different objects.
The StringBuilder.append method is an example of the
builder pattern since it allows you to build a StringBuilder
object using various data types like int, long etc. Also, if you
change the parameter passed to this object, you will get a
different object.

2: What are the types of Java Design Patterns? Explain each one
in detail.
Answer:
There are three types of design patterns supported by Java. They
are as follows:
a) Creational – These design patterns provide different ways
of creating an object of a class without using the new
operator. Some examples are Factory method, Prototype,
Singleton, etc.
b) Behavioral – These design patterns control how objects
communicate with each other and assign responsibility to
objects. Some examples are Chain of Responsibility, Iterator,
Mediator, etc.
c) Structural – These design patterns help to change the
structure of an object or convert the object into a different
form. Example Adapter, Decorator, Proxy, etc.
3: What are the types of J2EE Design Patterns? Give some
examples of each.
Answer:
J2EE Design Patterns can be classified based on the tier on which
they operate that is Design patterns for Presentation Tier, Business
Tier and Integration tier. Some examples of each are as follows:
a) Presentation Tier – Front Controller Design Pattern, View
Helper Design pattern, Intercepting Filter Design Pattern,
Context Object Design Pattern, etc.
b) Business Tier – Business Delegate Design Pattern, Data
Transfer Object Design Pattern, Service Locator Design
Pattern, Value List Handler Design Pattern, etc.
c) Integration Tier – Data Access Object Pattern and Service
Activator Pattern.

4: Name some J2EE technologies that use various design


patterns.
Answer:
a) Spring's Dispatcher Servlet uses the FrontController
design pattern. In this design pattern, there is a single
centralized controller that receives client requests, does
some pre-processing and then hands over the request to the
actual services. In a Spring MVC application, the Dispatcher
Servlet is configured in web.xml and all incoming URLs are
mapped to it. The Dispatcher Servlet then passes the request
to the appropriate controller.
b) Apache Tiles framework makes use of the Composite View
design pattern. In this design pattern, a page is comprised of
multiple independent components, so it is easier to update
the content in each part of the page without affecting the
layout. The Apache Tiles framework allows you to write
code as simple fragments which can be then included in
pages as desired.
c) JMS uses the Service Activator pattern. This design pattern
makes asynchronous communication between a client and a
server application possible, so the client application does not
have to wait even if there is a long running process on the
server side. In a JMS application, there is a JMS Provider that
provides services asynchronously to client applications.

5: What are Design Patterns?


Answer:
Design Patterns as the name suggests are the series of patterns
which can be applied to software problems which are of repeating
nature. These Design patterns offer probable solutions to these
problems through the use of classes and methods in an object-
oriented programming environment. The Design Patterns have
been designed and tested by experienced engineers over the years
and serve as a template to solve the problems encountered in
software engineering.

6: What is a Class Interface in Design Patterns?


Answer:
A Class Interface represents the collection of Methods and fields of
a class that allow objects of other classes to access.
7: What is a Class Implementation in Design Patterns?
Answer:
A Class Implementation is designed to be the code that lies within
its own code, with instructions on how an object is to fulfill a
certain commitment.

8: What is the Java Interface Construct?


Answer:
The Java Interface Construct is a tool, powerful in its own right,
used for adaptation of class interfaces and for applying patterns to
collection of classes.

9: What is a Stub in Design Patterns?


Answer:
A Stub is a class used to implement the interface with Methods
that do absolutely nothing; developers use this as a way to
override Methods in the interface that are important to their own
applications.

10: What is a Constant in Design Patterns?


Answer:
A Constant is a field that is static and final and it is defined by
interfaces and classes that are cooperating with each other when it
comes to the process of defining it.

11: What is the purpose of Design Patterns?


Answer:
Design Patterns are used mainly for solving problems that occur
in a context, with interface oriented patterns addressing the
context in which developers need to define or redefine access to
the Methods of a class or group of classes.

12: What is an Adapter in Design Patterns?


Answer:
An Adapter is used for implementing expected interfaces, by
having the developer provide the interface that a client needs,
using the services of a class with a different interface.

13: What are measures and dimensions in Design Patterns?


Answer:
a) Measures are a combination of magnitude and dimension.
b) Dimensions are an aggregation of a measure’s length, mass
and time exponents.

14: What does the times() method do in Design Patterns?


Answer:
The times() method is a standard measure, part of a unit, that
instantiates whichever class of Measure reflects the dimension of
any given unit.

15: When is Abstract Class used in Design Patterns?


Answer:
An Abstract Class is used when a Java interface is unusable and it
can be deployed by having an object adapter that uses delegation
as a Method rather than subclassing.
16: What does JDK stand for?
Answer:
JDK stands for Java Development Kit. It is used to supply an
Abstract Class that provides default implementation to most of
the domain specific Methods.

17: What does the JTable class do in Design Patterns?


Answer:
The JTable class is used to handle almost every aspect of
displaying a table, but cannot know in advance what data the
developer wishes to present; it is a main setter for the use of the
Adapter.

18: What is a Facade in Design Patterns?


Answer:
A Facade is a class with a level of functionality that lies between a
toolkit and a complete application, thus offering a vanilla usage of
the classes in a package or even in a subsystem. It is mainly used
to provide an interface that makes a subsystem easy to use, if it is
deployed as a pattern.

19: What is a Demo in Design Patterns?


Answer:
A Demo is an example that provides the same value as a Facade
and shows the use of a class or a subsystem. It is mainly a stand
alone, non-reusable application that shows on way to apply a
subsystem.
20: What is a Composite in Design Patterns?
Answer:
A Composite is a group of objects in which objects may contain
other objects, such as one object may represent groups and
another object may represent an individual item.

21: What is the purpose of a Composite Pattern?


Answer:
A Composite Pattern is used to let clients treat individual objects
and compositions of objects in a tight, uniform way, by defining a
common type for groups and items and by modeling groups as
containing a collection of
objects of a certain type.

22: What are Directed Graphs in Design Patterns?


Answer:
A Directed Graph is an object reference that has a direction with the
tree term applied to undirected graphs. For a directed graph to be
considered a tree it has to have a root node that has no references
to it and it also has to have one edge to all other nodes.
Chapter 2

Methods and Classes

23: What does the getStepCount() Method do in Design Patterns?


Answer:
This Method is used as an operation in the Process Component class
to count the number of steps needed in a process flow; it has to
count each step only once and to not enter an infinite loop when a
process contains a cycle.

24: What is the danger of Modeling Composites in Design


Patterns?
Answer:
Because Modeling Composites usually leads to recursive definition
of Methods on composite nodes, a danger exists of writing code
that may produce an infinite loop.
25: What is an Abstraction in Design Patterns?
Answer:
Abstraction is an oops concept where necessary details are hidden
from the user, and only relevant details are exposed to the user. It
helps in designing a solution to the problem during the design
phase. Abstraction can be achieved by an interface or an abstract
class. The abstract class consists of abstract methods without
providing the concrete implementations for the abstract methods.
It can also consist of non-abstract methods with core
implementation. While interface only consists of abstract methods
without their implementation.

26: How can we create a Bridge in Design Patterns?


Answer:
In order to create a Bridge, a set of abstract methods are added to
an interface. This helps to achieve abstraction by decoupling the
implementation of the abstract methods. The abstraction depends
on the implementation carried by the class implementing the
interface.

27: What is a Driver in Design Patterns?


Answer:
An Object that helps to interact with the computer system or an
external device is known as a Driver. It is created as per the
specification provided by the interface. In design patterns, a Driver
can be considered as an instance of the Adapter Pattern.
28: Why is UML used for Design Patterns?
Answer:
UML is an application modeling specification from OMG that
simplifies the complex software engineering process.

29: How many diagrams does UML offer for classification in


Design Patterns?
Answer:
UML offers twelve diagrams that represent an application’s
requirements analysis and solution design.

30: How many structure diagrams does UML provide?


Answer:
UML provides four structure diagrams that are used to represent
a static structure of an application. They are:
a) Class,
b) Object,
c) Component and
d) Deployment Diagrams.

31: How many Behavior Diagrams does UML provide?


Answer:
There are seven Behavioral Diagrams provided by UML to
represent the different behavioral aspects of an application. They
are:
a) Activity
b) Interaction Overview
c) Timing
d) State
e) Use case,
f) Sequence and,
g) Collaboration

32: How many Model Management diagrams does UML


provide?
Answer:
There are three Model Management Diagrams provided by UML
to represent the organization and management of various
application modules. They are:
a) Packages,
b) Subsystems and
c) Models diagrams

33: What are the Class Diagrams?


Answer:
Class Diagrams are considered as a blueprint of any application
and provide a structure for the system. It consists of objects,
behavior, and association with the other class objects depicted in
the form of a class diagram.

34: What is an Inner Class?


Answer:
An Inner Class is a class that is simply defined inside another class
with the concept of an inner class existing in object oriented
languages like Java, C++and C#.
35: What is the Generalization Method used for in Design
Patterns?
Answer:
Generalization is mainly used to depict the Object Oriented
Concept of Inheritance when there is a base class with common
behavior and each of its derived classes contains specific details.

36: What is an Interface in Design Patterns?


Answer:
An Interface is used to specify the externally visible operations of a
class and not the actual implementations of those operations; it
also most of the times specifies only a part of the behavior of an
actual implementer class.

37: What is Realization in Design Patterns?


Answer:
Realization is used to show the relationship between an interface
and a class that provides the actual implementation and it can be
drawn in two ways depending on how the interface is depicted.

38: What is Dependency in Design Patterns?


Answer:
Dependency is used to show the relationship between a source and
a target component when there is a dependency between relations
between the two.
39: What is Multiplicity in Design Patterns?
Answer:
Multiplicity is used to indicate the number of instances of one class
linked to one instance of the other class, with different values used
to indicate the Multiplicity.

40: What is a Message in Design Patterns?


Answer:
A Message simply puts communication between two objects with
the horizontal line indicating a message that can be labeled with
the name of the operation along with its argument values.

41: Why are Private Methods used for in Design Patterns?


Answer:
Private Methods are used to provide means of designing class
behaviors so that external objects are not permitted to access the
behavior that is meant only for an internal use.

You might also like