Question 41
MediumWhy does dependency injection make unit testing easier?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
Why does dependency injection make unit testing easier?
A fake or mock implementation can be passed in place of the real database or service
It removes the need for tests
It runs tests automatically
It makes all methods public
Sign up free to see the answer and a step-by-step explanation.
Which statement best describes the Liskov Substitution Principle?
Subclasses must not add new methods
Every class must have one method
Interfaces must be small
Objects of a subclass should be replaceable for objects of the parent without breaking correct behaviour
Sign up free to see the answer and a step-by-step explanation.
A Bird class has fly(). Ostrich extends Bird. Which redesign follows LSP?
Remove the Ostrich class
Bird without fly(), with a FlyingBird subclass or Flyable interface for birds that fly
Ostrich.fly() does nothing silently
Ostrich.fly() throws an exception
Sign up free to see the answer and a step-by-step explanation.
Which change violates the Open/Closed Principle?
Adding a new class that implements TaxRule
Writing a new test
Editing a tested TaxCalculator class every time a new country is supported
Registering a new TaxRule in configuration
Sign up free to see the answer and a step-by-step explanation.
Which design pattern is often used to apply the Open/Closed Principle to algorithms that vary, such as discount rules?
Prototype
Strategy
Facade
Singleton
Sign up free to see the answer and a step-by-step explanation.
What does DRY (Don't Repeat Yourself) recommend?
Each piece of knowledge should have a single, clear place in the code
Never write comments
Repeat code to make it clearer
Never use loops
Sign up free to see the answer and a step-by-step explanation.
What does KISS recommend?
Always use design patterns
Keep designs as simple as possible
Use inheritance everywhere
Keep every class small by splitting all methods
Sign up free to see the answer and a step-by-step explanation.
A developer adds a plug-in system 'in case we need it someday', though no one has asked for plug-ins. Which principle warns against this?
LSP
YAGNI
DRY
ISP
Sign up free to see the answer and a step-by-step explanation.
What is high cohesion?
The parts of a class or module are closely related and work towards one purpose
Every class has many responsibilities
Classes depend heavily on each other
All code is in one file
Sign up free to see the answer and a step-by-step explanation.
What is low coupling?
Modules never communicate
Modules share all their data
Every class inherits from every other
Modules depend on each other as little as possible, through clear interfaces
Sign up free to see the answer and a step-by-step explanation.