What is the Open/Closed Principle in software design?
It means software entities like classes or modules should be open for extension but closed for modification. You can add new features without changing existing code.
Click to reveal answer
beginner
Why is the Open/Closed Principle important?
It helps keep code stable and reduces bugs by avoiding changes to tested code. It also makes adding new features easier and safer.
Click to reveal answer
intermediate
How can you apply the Open/Closed Principle in a system?
Use interfaces or abstract classes to define behavior, then add new classes to extend functionality without changing existing ones.
Click to reveal answer
beginner
Give a real-life example of the Open/Closed Principle.
Think of a smartphone app that supports new payment methods by adding new modules, without changing the core app code.
Click to reveal answer
intermediate
What can happen if you ignore the Open/Closed Principle?
You risk breaking existing features when adding new ones, making the system fragile and harder to maintain.
Click to reveal answer
What does 'closed for modification' mean in the Open/Closed Principle?
AYou should not change existing tested code.
BYou should never add new features.
CYou must rewrite code for every change.
DYou should delete old code frequently.
✗ Incorrect
Closed for modification means existing code should not be changed to avoid introducing bugs.
How do you make a system 'open for extension'?
ABy avoiding new features.
BBy rewriting all code from scratch.
CBy deleting old features.
DBy adding new code without changing existing code.
✗ Incorrect
Open for extension means you can add new features by adding new code, not changing old code.
Which design tool helps implement the Open/Closed Principle?
AInterfaces or abstract classes
BGlobal variables
CHard-coded values
DCopy-pasting code
✗ Incorrect
Interfaces and abstract classes allow adding new behaviors without modifying existing code.
Ignoring the Open/Closed Principle can lead to:
AMore stable software
BFragile code that breaks easily
CFaster development always
DLess bugs automatically
✗ Incorrect
Changing existing code often introduces bugs, making the system fragile.
Which of these is a good practice for Open/Closed Principle?
AAvoid using interfaces
BModify existing classes for new features
CAdd new classes to extend behavior
DWrite all code in one big class
✗ Incorrect
Adding new classes to extend behavior keeps existing code unchanged.
Explain the Open/Closed Principle and why it matters in software design.
Think about how you can add features without breaking existing code.
You got /3 concepts.
Describe a simple example where applying the Open/Closed Principle improves a system.
Imagine adding a new payment method to an app without changing old code.
You got /3 concepts.
Practice
(1/5)
1. What does the Open/Closed Principle (OCP) primarily encourage in software design?
easy
A. Software entities should be open for extension but closed for modification.
B. Software entities should be open for modification but closed for extension.
C. Software entities should be neither open nor closed for changes.
D. Software entities should always be rewritten for new features.
Solution
Step 1: Understand the definition of OCP
The Open/Closed Principle states that software components should allow new functionality to be added without changing existing code.
Step 2: Match the principle to the options
Software entities should be open for extension but closed for modification correctly states that entities should be open for extension (new features) but closed for modification (existing code remains unchanged).
Final Answer:
Software entities should be open for extension but closed for modification. -> Option A
Quick Check:
OCP = Open for extension, closed for modification [OK]
Hint: Remember: extend without changing existing code [OK]
Common Mistakes:
Confusing 'open for extension' with 'open for modification'
Thinking OCP means rewriting code for new features
Ignoring the 'closed for modification' part
2. Which of the following code structures best supports the Open/Closed Principle?
easy
A. Using inheritance or interfaces to add new behavior without changing existing code.
B. Hardcoding all features inside one large class.
C. Copying and pasting code to create new features.
D. Modifying existing classes directly to add new features.
Solution
Step 1: Identify structures that allow extension without modification
Inheritance and interfaces allow new classes to add behavior without changing existing classes.
Step 2: Evaluate options against OCP
Using inheritance or interfaces to add new behavior without changing existing code uses inheritance/interfaces, which aligns with OCP. Other options involve modifying or duplicating code, violating OCP.
Final Answer:
Using inheritance or interfaces to add new behavior without changing existing code. -> Option A
Quick Check:
Inheritance/interfaces = OCP support [OK]
Hint: Choose inheritance or interfaces for extension [OK]
A. Liskov Substitution Principle by replacing shapes with unrelated classes.
B. Single Responsibility Principle by separating area calculation.
C. Dependency Inversion Principle by depending on concrete classes.
D. Open/Closed Principle by allowing new shapes without modifying totalArea.
Solution
Step 1: Analyze how new shapes are added
New shape classes (Rectangle, Circle) extend Shape and implement area(). totalArea works without changes.
Step 2: Match design to OCP
Since totalArea does not change when new shapes are added, the design follows Open/Closed Principle.
Final Answer:
Open/Closed Principle by allowing new shapes without modifying totalArea. -> Option D
Quick Check:
Adding shapes without changing totalArea = OCP [OK]
Hint: Look for unchanged code when adding new features [OK]
Common Mistakes:
Confusing OCP with other SOLID principles
Thinking totalArea must change for new shapes
Ignoring inheritance usage
4. Given this code violates the Open/Closed Principle:
class Report {
generate(type) {
if (type === 'pdf') {
// generate PDF
} else if (type === 'html') {
// generate HTML
}
}
}
What is the best fix to comply with OCP?
medium
A. Add more if-else cases inside generate() for new report types.
B. Create separate classes for each report type implementing a common interface.
C. Remove the generate() method and write report code inline.
D. Use global variables to control report type.
Solution
Step 1: Identify why current code violates OCP
Adding new report types requires modifying generate() method, breaking OCP.
Step 2: Choose design that allows extension without modification
Separate classes for each report type implementing a common interface allow adding new types without changing existing code.
Final Answer:
Create separate classes for each report type implementing a common interface. -> Option B
Quick Check:
Separate classes = extend without modify [OK]
Hint: Use separate classes, not if-else chains [OK]
Common Mistakes:
Adding more if-else blocks inside methods
Ignoring interface or inheritance use
Using global variables for behavior control
5. You are designing a payment system that must support new payment methods without changing existing code. Which approach best follows the Open/Closed Principle?
hard
A. Rewrite the payment processor each time a new method is added.
B. Add new payment method code inside a large switch statement in the payment processor.
C. Implement a Payment interface and create new classes for each payment method.
D. Use global flags to select payment methods inside one class.
Solution
Step 1: Understand the requirement for extensibility without modification
The system must allow adding new payment methods without changing existing code.
Step 2: Evaluate design options for OCP compliance
Implementing a Payment interface and creating new classes for each method allows extension by adding classes, not modifying existing ones.
Final Answer:
Implement a Payment interface and create new classes for each payment method. -> Option C
Quick Check:
Interface + new classes = OCP compliance [OK]
Hint: Use interfaces and new classes for new features [OK]
Common Mistakes:
Using switch statements that require modification
Rewriting existing code repeatedly
Using global flags for behavior control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.