Single Responsibility Principle (SRP)
A class should have one reason to change. Identify SRP violations and learn how to split responsibilities correctly.
Questions in this quiz
10 questions on Single Responsibility Principle (SRP). Work through them here, then start the quiz to answer them and see your score.
1.What does the Single Responsibility Principle state?
- AA class should have at most one method
- BA class should be as small as possible
- CA class should have only one reason to change — it owns one distinct responsibility
- DAll classes must be immutable
2.Which of the following classes most clearly violates SRP?
- APasswordHasher — only hashes and verifies passwords
- BPaymentService — processes payments, formats email receipts, generates PDF invoices, and logs audit trails
- CUserRepository — handles only database CRUD for users
- DOrder — represents an order's data fields
3.A class contains both business logic and database queries. What SRP-aligned pattern should you apply?
- AMake the class abstract
- BExtract data access into a Repository or DAO class
- CKeep them together for simplicity — separation adds unnecessary complexity
- DAdd more methods to handle both concerns better
4.This class violates SRP. What should be extracted?
class ReportService { public Report generateReport(List<Data> data) { ... } public String formatAsHtml(Report r) { ... } public void saveToDatabase(Report r) { ... } public void sendByEmail(Report r, String to) { ... } }- AAll four methods belong together for cohesion
- BOnly sendByEmail should be moved to a separate class
- CformatAsHtml → ReportFormatter, saveToDatabase → ReportRepository, sendByEmail → EmailService
- DNothing — this class is fine
5.What is the relationship between SRP and testability?
- ASRP makes classes harder to test because you need more test files
- BSRP is completely unrelated to testing
- CClasses with a single responsibility have fewer dependencies to mock and simpler behaviour — making unit tests smaller and more focused
- DFollowing SRP means you don't need unit tests
6.A User class handles authentication, profile management, and email notifications. How many reasons to change does it have?
- ACannot determine without code
- B3 — authentication, profile, and notifications
- C2 — authentication and profile
- D1 — it's all about the user
7.A logger class that writes to files, formats log messages, and manages log rotation — how many responsibilities is that?
- A3 — writing, formatting, and rotation
- B2 — writing and formatting
- C1 — it's all logging
- D4 — a new responsibility for each log level
8.What does "reason to change" actually mean in practice?
- AThe class is too large and must be refactored
- BThe class has more than one method
- CThe class has a bug that needs fixing
- DThere is a different stakeholder, requirement, or concern that could independently cause this class to need modification
9.Which of these is NOT a sign of an SRP violation?
- AA class has a single private helper method
- BA class imports from many unrelated external packages (e.g., HTTP client, PDF library, database ORM)
- CA class has many tests that test completely different behaviours
- DA class is very long with methods covering unrelated topics
10.SRP says "one responsibility" — does that mean a class can only have one method?
- ANo — a class can have many methods as long as they all serve the same single purpose
- BYes — SRP and the number of methods are directly linked
- CNo — SRP only applies to interfaces, not classes
- DYes — one method per class is the strictest interpretation