CS FundamentalsModerateTCS Digital · Infosys

A class Invoice has methods calculateTotal(), saveToDatabase() and renderPdf(). Which principle does it violate, and what is the usual fix?

  1. A. Liskov Substitution; make Invoice abstract
  2. B. Interface Segregation; split the interface into three
  3. C. Single Responsibility; it has three reasons to change (business rules, persistence, presentation), so move persistence and rendering into separate classes
  4. D. Open/Closed; add a switch statement for output formats

Correct answer: C. Single Responsibility; it has three reasons to change (business rules, persistence, presentation), so move persistence and rendering into separate classes

SRP is about reasons to change: tax rules, database schema and PDF layout change for different stakeholders. Splitting into Invoice, InvoiceRepository and InvoiceRenderer isolates them. ISP is about clients being forced to depend on methods they don't use.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic