CPSA Software Design Principles 2 — Questions and Answers
Question 1: Which design principle states that a class should have only one reason to change?
- Open/Closed Principle
- Single Responsibility Principle (Correct answer)
- Liskov Substitution Principle
- Interface Segregation Principle
Correct answer: Single Responsibility Principle
The Single Responsibility Principle (SRP) states that a class should have only one reason to change, meaning it should have only one responsibility.
Question 2: In the context of software coupling, which scenario represents the LOWEST level of coupling?
- Two modules share a global data structure
- One module passes a single primitive value to another (Correct answer)
- One module directly modifies another's internal state
- Two modules communicate via a common database table
Correct answer: One module passes a single primitive value to another
Data coupling, where modules interact only through simple parameters, represents the lowest (loosest) form of coupling.
Question 3: Which principle guides you to 'program to an interface, not an implementation'?
- Dependency Inversion Principle (Correct answer)
- Single Responsibility Principle
- Law of Demeter
- Composition over Inheritance
Correct answer: Dependency Inversion Principle
The Dependency Inversion Principle advocates programming to abstractions (interfaces) rather than concrete implementations to reduce high-level module dependency on low-level details.
Question 4: A software team notices that changing one module frequently requires changes in many other unrelated modules. This is best described as a violation of which principle?
- DRY (Don't Repeat Yourself)
- High Cohesion
- Low Coupling (Correct answer)
- YAGNI
Correct answer: Low Coupling
When changes ripple across many modules, it indicates high coupling — modules are too tightly interdependent, violating the Low Coupling principle.
Question 5: The Law of Demeter (Principle of Least Knowledge) primarily aims to reduce which quality problem?
- Low cohesion within a class
- Tight coupling between objects (Correct answer)
- Code duplication across modules
- Insufficient abstraction layers
Correct answer: Tight coupling between objects
The Law of Demeter limits object interactions to immediate collaborators, reducing tight coupling by preventing deep chains of method calls.
Question 6: Which of the following BEST describes the concept of information hiding in software design?
- Encrypting sensitive data within a module
- Concealing implementation details behind a public interface (Correct answer)
- Restricting access rights to source code files
- Hiding error messages from end users
Correct answer: Concealing implementation details behind a public interface
Information hiding means concealing a module's internal implementation details, exposing only a well-defined interface to reduce coupling and support change.
Question 7: When applying the 'Separation of Concerns' principle, which outcome is MOST expected?
- Faster compilation times due to smaller files
- Each module addresses a distinct, well-defined aspect of functionality (Correct answer)
- Elimination of all runtime errors
- Modules that can run independently without any integration
Correct answer: Each module addresses a distinct, well-defined aspect of functionality
Separation of Concerns divides a system so each module addresses one distinct aspect, improving maintainability and reducing complexity.
Which design principle states that a class should have only one reason to change?