Question 61
HardA ReportGenerator class formats data, writes PDF files and emails them. A change in email settings forces a retest of PDF formatting. 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
A ReportGenerator class formats data, writes PDF files and emails them. A change in email settings forces a retest of PDF formatting. Which principle is violated?
Single Responsibility
Dependency Inversion
Liskov Substitution
Interface Segregation
Sign up free to see the answer and a step-by-step explanation.
A discount calculator has a growing switch on customer type, edited for every new type. Which refactoring follows Open/Closed?
A DiscountPolicy interface with one class per customer type, chosen through a map or factory
Using global variables
Copying the calculator per type
Adding more cases to the switch
Sign up free to see the answer and a step-by-step explanation.
A subclass of a 'Bird' class must implement fly() but penguins can't fly, so it throws an exception. Which principle and fix apply?
Liskov Substitution; separate flying birds into a FlyingBird type or interface
Open/Closed; add an if statement
YAGNI; delete penguins
DRY; copy the code
Sign up free to see the answer and a step-by-step explanation.
An IMachine interface has print(), scan(), fax() and staple(). A simple printer implements three of them as empty methods. Which principle is broken?
Single Responsibility
Dependency Inversion
Interface Segregation
Liskov Substitution
Sign up free to see the answer and a step-by-step explanation.
A PaymentService does new StripeGateway() inside its methods. Which change follows Dependency Inversion?
Make PaymentService a subclass of StripeGateway
Make StripeGateway static
Copy StripeGateway's code into PaymentService
Depend on a PaymentGateway interface and receive the implementation through the constructor
Sign up free to see the answer and a step-by-step explanation.
Which of these violates the Liskov Substitution Principle?
A subclass method that accepts a narrower range of inputs than the parent method
A subclass that calls super()
A subclass that returns a more specific type
A subclass that adds a new method
Sign up free to see the answer and a step-by-step explanation.
Which design pattern lets you add behaviour like logging or caching around an object without changing its class?
Decorator
Prototype
Builder
Singleton
Sign up free to see the answer and a step-by-step explanation.
Which pattern provides a placeholder that controls access to another object, such as lazy loading or permission checks?
Composite
Observer
Proxy
Adapter
Sign up free to see the answer and a step-by-step explanation.
Which pattern lets you treat individual objects and groups of objects in the same way, such as files and folders?
Composite
Strategy
Flyweight
Facade
Sign up free to see the answer and a step-by-step explanation.
Which pattern shares common parts of many similar objects to save memory, such as character glyphs in a text editor?
Bridge
Prototype
Flyweight
Decorator
Sign up free to see the answer and a step-by-step explanation.