QuizzesOpen/Closed Principle (OCP)
intermediate
solid

Open/Closed Principle (OCP)

Open for extension, closed for modification — add features by writing new code, not editing existing tested code.

20 minutes 10 questions Pass at 70%
Time Limit
20 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

10 questions on Open/Closed Principle (OCP). Work through them here, then start the quiz to answer them and see your score.

  1. 1.The Open/Closed Principle states software entities should be:

    • AOpen for extension (add new behaviour) but closed for modification (don't edit existing tested code)
    • BClosed for inheritance and open for composition
    • COpen for testing and closed for deployment
    • DOpen for public access and closed for private access
  2. 2.What OCP violation does this code have?

    class DiscountCalculator {
        public double calculate(String type, double price) {
            if (type.equals("student")) return price * 0.8;
            if (type.equals("senior")) return price * 0.7;
            if (type.equals("employee")) return price * 0.6;
            return price;
        }
    }
    • ANo violation — this is clean code
    • BThe class should extend an abstract Discount class
    • CThe method name should be "computeDiscount"
    • DAdding a new discount type requires modifying this existing class, violating OCP
  3. 3.Which design patterns most directly enable the Open/Closed Principle?

    • ABuilder and Flyweight
    • BStrategy, Template Method, Decorator, and Observer
    • CDAO and Repository
    • DSingleton and Factory
  4. 4.A logger currently writes to files. Now it needs to write to a database and cloud. OCP-compliant approach:

    • AAdd if/else branches in Logger for each new destination
    • BDefine a LogSink interface; create FileSink, DatabaseSink, CloudSink; inject the chosen sink into Logger
    • CCreate separate subclasses: FileLogger, DatabaseLogger, CloudLogger — subclassing is the only valid OCP approach
    • DAdd a type parameter to Logger's constructor
  5. 5.What is the difference between "extending" and "modifying" in the context of OCP?

    • AExtending: writing new code (new class, new implementation) to add behaviour; Modifying: editing existing, deployed, tested code
    • BThey are the same — both change the class's runtime behaviour
    • COCP only applies to final classes that cannot be subclassed
    • DExtending uses inheritance; modifying uses composition
  6. 6.A Shape area calculator uses a switch statement on shape type. What OCP-compliant refactoring applies?

    • AMake AreaCalculator abstract and subclass it per shape
    • BReplace switch with if/else — that is more readable
    • CMove the switch to a different class
    • DDefine a Shape interface with an area() method; each shape implements its own area() calculation
  7. 7.Can OCP be achieved purely through inheritance without using interfaces?

    • ANo — only interfaces enable OCP
    • BYes — but composition via interfaces is often more flexible and doesn't couple to a specific inheritance hierarchy
    • CNo — OCP requires both inheritance and interfaces together
    • DYes — inheritance is the only way to achieve OCP
  8. 8.A ReportGenerator currently supports PDF output. Adding JSON output violates OCP if done how?

    • AInjecting a ReportFormatter dependency
    • BCreating a new JsonReportGenerator class
    • CAdding a format parameter and if/else inside the existing generateReport() method
    • DDefining an OutputFormat interface and implementing JsonOutputFormat
  9. 9.Why is "the same class needs to be changed every time a new requirement arrives" a warning sign?

    • AIt indicates the class is not closed for modification — it should be restructured so new requirements arrive as extensions, not edits
    • BIt only matters for public API classes
    • CIt means the class is too small and should be merged with others
    • DIt is not a warning sign — frequent changes show an active codebase
  10. 10.Does OCP mean you should never modify existing code once it is written?

    • AYes — once code is written it must never change
    • BNo — OCP is about designing so that adding new behaviour doesn't require modifying stable, tested code; bug fixes and refactoring are always acceptable
    • CYes — but only for public APIs
    • DNo — OCP only applies to abstract classes