Master Design Patterns in Java with 30 MCQs | Enhance Your Software Design Techniques
Improve your understanding of Java design patterns like Singleton, Factory, Builder, Observer, Strategy, Adapter, Decorator, and MVC Architecture. Perfect for software developers looking to level up their design pattern skills.
MCQs
1-10: Singleton, Factory, and Builder Patterns
The Singleton design pattern ensures that:
A) Only one instance of a class is created
B) Multiple instances of a class are created
C) The class is abstract
D) The class can be inherited
The purpose of the Factory Method pattern is to:
A) Provide a way to create objects without specifying the exact class of object
B) Hide the implementation details of the class
C) Define a family of algorithms
D) Adapt one interface to another
Which of the following is an advantage of using the Singleton pattern?
A) Increased object creation
B) Ensured single instance of a class
C) Easier inheritance
D) Improved method overriding
The Builder design pattern is mainly used to:
A) Create complex objects with a simple and flexible construction process
B) Abstract the database connection details
C) Simplify object inheritance
D) Provide access to multiple instances
In which scenario would you most likely use the Factory design pattern?
A) When the exact class to instantiate is determined at runtime
B) When a single instance of a class is needed
C) When a class needs to be inherited
D) When complex object creation is involved
The Singleton pattern can be implemented using:
A) A constructor with a private modifier
B) A static factory method
C) Both A and B
D) An abstract class
Which of the following statements about the Builder pattern is true?
A) It separates the construction of a complex object from its representation
B) It allows direct modification of object properties
C) It combines object creation and the client interface
D) It is only useful for simple objects
In the Factory design pattern, the client is responsible for:
A) Creating the product
B) Defining the product interface
C) Requesting the factory to create the product
D) None of the above
The Singleton pattern ensures that a class has:
A) No constructors
B) A static reference to its single instance
C) Multiple instances
D) An interface for implementation
What is the main benefit of using the Builder design pattern?
A) It simplifies object creation for complex objects
B) It ensures object immutability
C) It provides an abstract interface for object creation
D) It reduces the number of classes in the codebase
11-20: Observer and Strategy Patterns
The Observer design pattern is primarily used to:
A) Define one-to-many dependencies where a change in one object triggers updates to other dependent objects
B) Allow multiple classes to inherit from a single class
C) Organize algorithms into interchangeable families
D) Attach additional responsibilities to an object dynamically
Which of the following describes the Strategy pattern?
A) A way to create an object without specifying its exact class
B) A method for encapsulating interchangeable algorithms
C) A way to maintain a single instance of an object
D) A pattern used to extend functionality without changing the class
The Observer pattern is typically used in scenarios where:
A) Changes in one object need to be reflected in multiple other objects automatically
B) A client class needs to determine which algorithm to use
C) Object inheritance is required
D) A single class must handle multiple types of requests
Which class in the Observer pattern maintains a list of observers?
A) Observable
B) Subject
C) ConcreteObserver
D) Strategy
The Strategy pattern allows a client to:
A) Change the algorithm at runtime
B) Define a global policy for object behavior
C) Share code between classes
D) Change the number of observers
In the Observer pattern, which role does the Observer class play?
A) It sends updates to the subject
B) It receives notifications from the subject
C) It manages algorithm selection
D) It defines the contract for the subject
The Strategy pattern is most useful when:
A) Different algorithms can be selected at runtime depending on the situation
B) You need to manage a list of dependent objects
C) The same algorithm needs to be applied to many classes
D) You need to communicate between objects
In the Observer pattern, how are the updates communicated?
A) Through method calls on the Subject
B) By direct method invocation on Observers
C) By sending messages between Observer and Subject
D) By using dynamic class loading
The main goal of the Observer pattern is to:
A) Separate concerns between the observer and the object being observed
B) Allow for reusable code and multiple inheritance
C) Maintain a central place for object creation
D) Replace inheritance with composition
In the Strategy pattern, the context object:
A) Decides which algorithm to use at runtime
B) Encapsulates the family of algorithms
C) Changes its algorithm during initialization
D) Defines a public interface for algorithms
21-30: Adapter and Decorator Patterns
The Adapter pattern allows incompatible interfaces to:
A) Be modified
B) Be extended without changing their code
C) Be adapted to a common interface
D) Be combined into one interface
The Decorator pattern is used to:
A) Dynamically add new behavior to an object
B) Modify the object constructor
C) Create multiple subclasses for a class
D) Change the method signatures of a class
Which pattern allows a class to adapt its interface to the client’s needs without altering its existing code?
A) Adapter pattern
B) Strategy pattern
C) Factory pattern
D) Builder pattern
Which of the following is an example of the Adapter pattern in Java?
A) Wrapping an old class with a new interface
B) Changing the state of an object dynamically
C) Passing an object to another object in different form
D) Modifying an object’s behavior via inheritance
The Adapter pattern is useful when:
A) You need to integrate incompatible interfaces into a system
B) You need to swap out one algorithm for another
C) You want to maintain multiple instances of a class
D) You are changing the structure of an object
The Decorator pattern allows you to:
A) Add new responsibilities to an object dynamically
B) Create a new subclass for an object
C) Combine multiple behaviors into one object
D) Change the internal state of an object
Which pattern allows you to extend the functionality of a class without modifying its structure?
A) Adapter pattern
B) Decorator pattern
C) Singleton pattern
D) Proxy pattern
The Adapter pattern works by:
A) Changing the interface of an existing class
B) Creating a wrapper class that translates calls to the target class
C) Adding additional behavior to the original class
D) Modifying the constructor of an object
In which scenario would you most likely use the Decorator pattern?
A) When you need to add new features to an object without changing its class
B) When a class needs to be reused by multiple applications
C) When integrating two incompatible systems
D) When creating a single instance of a class
The Adapter pattern is typically used when:
A) A class needs to be adapted to a new interface
B) You need to build complex objects step-by-step
C) Different algorithms need to be applied at runtime
D) You need to share an algorithm between multiple classes
Answer Key
Qno
Answer
1
A) Only one instance of a class is created
2
A) Provide a way to create objects without specifying the exact class of object
3
B) Ensured single instance of a class
4
A) Create complex objects with a simple and flexible construction process
5
A) When the exact class to instantiate is determined at runtime
6
C) Both A and B
7
A) It separates the construction of a complex object from its representation
8
C) Requesting the factory to create the product
9
B) A static reference to its single instance
10
A) It simplifies object creation for complex objects
11
A) Define one-to-many dependencies where a change in one object triggers updates to other dependent objects
12
B) A method for encapsulating interchangeable algorithms
13
A) Changes in one object need to be reflected in multiple other objects automatically
14
B) Subject
15
A) Change the algorithm at runtime
16
B) It receives notifications from the subject
17
A) Different algorithms can be selected at runtime depending on the situation
18
B) By direct method invocation on Observers
19
A) Separate concerns between the observer and the object being observed
20
A) Decides which algorithm to use at runtime
21
C) Be adapted to a common interface
22
A) Dynamically add new behavior to an object
23
A) Adapter pattern
24
A) Wrapping an old class with a new interface
25
A) You need to integrate incompatible interfaces into a system
26
A) Add new responsibilities to an object dynamically
27
B) Decorator pattern
28
B) Creating a wrapper class that translates calls to the target class
29
A) When you need to add new features to an object without changing its class