CS FundamentalsModerateTCS Digital · Infosys
A class Invoice has methods calculateTotal(), saveToDatabase() and renderPdf(). Which principle does it violate, and what is the usual fix?
- A. Liskov Substitution; make Invoice abstract
- B. Interface Segregation; split the interface into three
- C. Single Responsibility; it has three reasons to change (business rules, persistence, presentation), so move persistence and rendering into separate classes
- 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.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- A Worker interface declares work(), eat() and sleep(). RobotWorker implements it and leaves eat() and sleep() throwing 'not supported'…
- How many of these subclass changes violate the Liskov Substitution Principle? 1. The overriding method accepts a wider range of inputs…
- An area calculator contains if shape.type == "circle": … elif shape.type == "square": …, and every new shape requires editing this…
- How many of these statements are correct? 1. Dependency Inversion is a design principle: high- and low-level modules should both depend on…
- Code contains order.getCustomer().getAddress().getCity().toUpperCase(). Which principle does it most directly violate, and why does it…
- A module reads a raw record, cleans it, and passes the cleaned record to a parser whose output is passed to a validator - each step's…