CPSA Software Design Principles 3 — Questions and Answers
Question 1: Which statement BEST describes the Open/Closed Principle?
- Classes should be open for modification and closed for extension
- Software entities should be open for extension but closed for modification (Correct answer)
- Public methods should be open while private methods remain closed
- Interfaces should be open to any implementation without restriction
Correct answer: Software entities should be open for extension but closed for modification
The Open/Closed Principle states that software entities should be open for extension (new behavior can be added) but closed for modification (existing code is not changed).
Question 2: A designer uses inheritance but the subclass cannot be used wherever the parent class is expected without breaking behavior. Which principle is violated?
- Open/Closed Principle
- Interface Segregation Principle
- Liskov Substitution Principle (Correct answer)
- Dependency Inversion Principle
Correct answer: Liskov Substitution Principle
The Liskov Substitution Principle requires that subtype objects must be substitutable for their supertype objects without altering program correctness.
Question 3: What is the PRIMARY benefit of applying the DRY (Don't Repeat Yourself) principle?
- Improved runtime execution speed
- Reduced risk of inconsistency when changing logic in one place (Correct answer)
- Smaller binary file sizes
- Better user interface responsiveness
Correct answer: Reduced risk of inconsistency when changing logic in one place
DRY reduces duplication so that when logic must change, it is updated in exactly one place, eliminating the risk of inconsistent copies.
Question 4: Which design heuristic does the statement 'You should not design for needs that do not yet exist' represent?
- SOLID
- YAGNI (You Aren't Gonna Need It) (Correct answer)
- DRY
- Separation of Concerns
Correct answer: YAGNI (You Aren't Gonna Need It)
YAGNI advises against implementing functionality until it is actually needed, avoiding unnecessary complexity and speculative design.
Question 5: In layered architecture, what design principle is enforced when higher layers only interact with the layer directly below them?
- Law of Demeter
- Single Responsibility Principle
- Layer isolation / strict layering (Correct answer)
- Composition over Inheritance
Correct answer: Layer isolation / strict layering
Strict layering enforces that each layer only communicates with the adjacent lower layer, maintaining clear boundaries and reducing coupling across layers.
Question 6: Which principle is MOST directly supported by using dependency injection?
- Single Responsibility Principle
- Liskov Substitution Principle
- Dependency Inversion Principle (Correct answer)
- Interface Segregation Principle
Correct answer: Dependency Inversion Principle
Dependency injection is a common technique for implementing the Dependency Inversion Principle, allowing high-level modules to depend on abstractions rather than concrete low-level classes.
Question 7: A class has methods for user authentication, report generation, and database connection management. Which principle does this MOST violate?
- Open/Closed Principle
- Single Responsibility Principle (Correct answer)
- Liskov Substitution Principle
- Interface Segregation Principle
Correct answer: Single Responsibility Principle
Bundling authentication, reporting, and database management into one class gives it multiple reasons to change, violating the Single Responsibility Principle.
Which statement BEST describes the Open/Closed Principle?