Prototype Pattern and Adapter Pattern are important Structural and Creational concepts asked in Java interviews. These patterns help solve problems related to object cloning, performance optimization, and compatibility between incompatible interfaces.
Q1. What is the primary purpose of the Prototype design pattern?
Options:
- A) To create a single instance of a class
- B) To create a copy of an existing object with minimal overhead
- C) To define an interface for creating families of related objects
- D) To separate object construction from its representation
Answer:
B) To create a copy of an existing object with minimal overhead
Explanation:
- A) Single instance: Incorrect. This describes the Singleton pattern.
- B) Copy of an existing object: Correct. Prototype is designed for creating clones of existing objects to avoid the cost of creating them from scratch.
- C) Families of related objects: Incorrect. This is the goal of the Abstract Factory pattern.
- D) Separate construction from representation: Incorrect. This describes the Builder pattern.
Q2. In a scenario where multiple instances of similar objects are required, why might the Prototype pattern be preferred over directly using constructors? (Select all that apply)
Options:
- A) It reduces the overhead of creating objects with complex initialization.
- B) It makes adding subclasses easier.
- C) It ensures the creation of only one instance of the object.
- D) It supports cloning objects with various configurations.
Answer:
A) It reduces the overhead of creating objects with complex initialization; D) It supports cloning objects with various configurations
Explanation:
- A) Reduces overhead: Correct. Cloning an existing object can be more efficient than complex instantiation.
- B) Adding subclasses: Incorrect. Prototype doesn’t directly aid subclassing.
- C) Single instance: Incorrect. This is a characteristic of Singleton.
- D) Cloning with configurations: Correct. Prototype allows the creation of objects with different configurations.
Q3. What is the primary purpose of the Adapter Pattern?
Options:
- A) To create an interface for incompatible classes to work together
- B) To add additional functionality to an object dynamically
- C) To restrict object creation to a single instance
- D) To provide a simplified interface to a complex system
Answer:
A
Explanation:
- A (Correct): The Adapter Pattern allows incompatible interfaces to work together by creating an intermediary adapter.
- B (Incorrect): This describes the Decorator Pattern.
- C (Incorrect): This describes the Singleton Pattern.
- D (Incorrect): This describes the Facade Pattern.
Q4. Which of the following roles does NOT directly participate in the Adapter Pattern?
Options:
- A) Adapter
- B) Target
- C) Client
- D) Factory
Answer:
D
Explanation:
- D (Correct): The Factory is not part of the Adapter Pattern structure.
- A, B, and C (Incorrect): The Adapter, Target, and Client are essential roles in the Adapter Pattern.
Q5. You have an old logging system that outputs logs in XML format, but your application now uses JSON for all logs. How can the Adapter Pattern help here?
Options:
- A) It can convert XML logs to JSON format without changing the old logging system
- B) It replaces the old logging system with a JSON-based one
- C) It combines XML and JSON logs into a single file
- D) It updates the old logging system to output JSON directly
Answer:
A
Explanation:
- A (Correct): The Adapter Pattern allows the old logging system to work with JSON by translating XML output to JSON.
- B (Incorrect): Replacing the system does not involve adapting the interface.
- C (Incorrect): This does not address compatibility.
- D (Incorrect): Updating the system isn’t necessary when using an adapter.
Why These Patterns Matter in Java Interviews
Interviewers ask Prototype and Adapter questions to test:
- Object cloning concepts
- Performance optimization
- Reusability
- Interface conversion
- Legacy system integration
- Loose coupling
- Real-world problem solving
Common Real-World Examples
Prototype Pattern:
- Game character cloning
- Resume templates
- Preconfigured system objects
- Document duplication
Adapter Pattern:
- Payment gateway integrations
- Legacy API compatibility
- XML to JSON conversion
- Third-party library integration
Quick Revision
- Prototype Pattern creates copies of existing objects.
- Adapter Pattern makes incompatible interfaces work together.
- Prototype belongs to Creational Patterns.
- Adapter belongs to Structural Patterns.
- Both are valuable in enterprise Java development.

