Question 31
MediumAn interface Worker has work() and eat(), and a Robot class must implement eat() with an empty body. Which principle is violated?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
An interface Worker has work() and eat(), and a Robot class must implement eat() with an empty body. Which principle is violated?
DRY
Interface Segregation
KISS
Liskov Substitution only
Sign up free to see the answer and a step-by-step explanation.
A ReportService creates a MySqlDatabase object directly inside its constructor. Which principle does this break, and what is the fix?
Open/Closed; make the class final
Dependency Inversion; depend on a Database interface passed in from outside
Single Responsibility; add more methods
Interface Segregation; remove the constructor
Sign up free to see the answer and a step-by-step explanation.
What is dependency injection?
Inheriting from many classes
Injecting code into a running program
Supplying an object's dependencies from outside (e.g. through its constructor) instead of creating them inside
Adding more libraries
Sign up free to see the answer and a step-by-step explanation.
What does YAGNI stand for?
Your Algorithm Gives No Information
You Aren't Gonna Need It: don't build features until they are needed
Yet Another Generic Interface
You Always Get New Ideas
Sign up free to see the answer and a step-by-step explanation.
A UserService class validates input, hashes passwords, sends welcome emails and writes logs. Which change best follows the Single Responsibility Principle?
Merge more features into UserService
Move hashing, emailing and logging into their own classes that UserService uses
Make every method static
Add more comments
Sign up free to see the answer and a step-by-step explanation.
A Shape area calculator uses if (type == "circle") … else if (type == "square") …, and every new shape needs this code edited. Which principle does a Shape interface with area() satisfy instead?
Single Responsibility only
Liskov Substitution
Interface Segregation
Open/Closed
Sign up free to see the answer and a step-by-step explanation.
A ReadOnlyFile class extends File but throws an exception from write(). Code that writes to any File now fails. Which principle is broken?
Open/Closed
Dependency Inversion
Single Responsibility
Liskov Substitution
Sign up free to see the answer and a step-by-step explanation.
A Printer interface has print(), scan(), fax() and staple(). A basic printer must implement all four, throwing errors for three. Which principle is broken?
Liskov Substitution
Open/Closed
Interface Segregation
DRY
Sign up free to see the answer and a step-by-step explanation.
An OrderService depends on an INotifier interface, and a concrete EmailNotifier is passed in from outside. Which principle does this follow?
Dependency Inversion
YAGNI
Interface Segregation
Single Responsibility
Sign up free to see the answer and a step-by-step explanation.
A NightlyReport class directly creates a new MySqlConnection() inside its run() method. Which change best follows Dependency Inversion?
Make NightlyReport take a DataSource interface in its constructor
Copy the connection code into every report
Make MySqlConnection a subclass of NightlyReport
Make run() static
Sign up free to see the answer and a step-by-step explanation.