Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the Prototype interface method for cloning.
LLD
interface Prototype {
Prototype [1]();
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names like copy or duplicate which are not standard in Prototype pattern.
Forgetting to return the Prototype type.
✗ Incorrect
The standard method name for creating a copy in the Prototype pattern is clone().
2fill in blank
mediumComplete the code to implement the clone method in a concrete prototype class.
LLD
class ConcretePrototype implements Prototype { private int id; ConcretePrototype(int id) { this.id = id; } public Prototype [1]() { return new ConcretePrototype(this.id); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than declared in the interface.
Not returning a new instance.
✗ Incorrect
The clone() method returns a new instance copying the current object's state.
3fill in blank
hardFix the error in the clone method to correctly copy the object state.
LLD
public Prototype clone() {
return new ConcretePrototype([1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables like prototype.id or new id.
Omitting 'this' causing ambiguity.
✗ Incorrect
The clone method must use this.id to copy the current object's id.
4fill in blank
hardFill both blanks to create a prototype registry and clone an object from it.
LLD
class PrototypeRegistry { private Map<String, Prototype> prototypes = new HashMap<>(); public void addPrototype(String key, Prototype prototype) { prototypes.put([1], [2]); } public Prototype getClone(String key) { return prototypes.get(key).clone(); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping key and prototype in the put method.
Using undefined variables.
✗ Incorrect
Use key as the map key and prototype as the value to store the prototype object.
5fill in blank
hardFill all three blanks to clone a prototype and modify its state.
LLD
Prototype original = registry.getClone([1]); ConcretePrototype copy = (ConcretePrototype) original; copy.[2] = [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect key strings.
Trying to call clone again instead of modifying the field.
Assigning a string to an integer field.
✗ Incorrect
Clone the prototype with key "item1", then set its id to 42.