Why is the Open/Closed Principle important in software development?
It increases the risk of introducing bugs during modification.
It promotes code duplication.
It makes code less readable.
It complicates the development process.
Consider a base class 'Bird' with a method 'fly()'. You create a subclass 'Penguin'. What would be the most LSP-compliant approach in this scenario?
Rethink the inheritance hierarchy. 'Bird' might not be the right superclass for 'Penguin' if flying is a core characteristic.
Have 'Penguin' inherit 'fly()' but leave it empty, as penguins don't need to fly.
Remove the 'fly()' method from the 'Bird' class entirely.
Have 'Penguin' inherit 'fly()' and throw an exception, as penguins cannot fly.
SOLID principles primarily aim to improve which aspect of software development?
Memory Optimization
Execution Speed
Algorithm Efficiency
Code Maintainability
How does using interfaces contribute to the Open/Closed Principle?
Interfaces allow for direct modification of existing class code.
Interfaces force tight coupling between classes.
Interfaces make code execution slower.
Interfaces define a contract that can be implemented by multiple classes, enabling flexibility.
Which of the following best describes the impact of SRP on code testability?
SRP makes unit testing easier as classes are smaller and have well-defined responsibilities.
SRP makes it more complex to write unit tests as classes have intertwined functionalities.
SRP makes it impossible to write unit tests as it encourages separation of concerns.
SRP has no significant impact on the ease or difficulty of writing unit tests.
How does the Interface Segregation Principle contribute to loose coupling in software design?
By minimizing dependencies between classes to only what is absolutely necessary.
By reducing the need for unit testing.
By encouraging the use of global variables for communication between classes.
By promoting the use of concrete classes instead of interfaces.
Which of these is NOT a potential consequence of violating the Liskov Substitution Principle?
Improved performance due to optimized subclass implementations.
Increased code complexity and reduced readability.
Decreased code reusability as subclasses may not behave as expected.
Higher likelihood of introducing bugs when extending or modifying code.
Imagine a class named 'Employee' that handles both employee data (like name, ID) and database operations. What SOLID principle does this violate?
Single Responsibility Principle
Interface Segregation Principle
Liskov Substitution Principle
Open/Closed Principle
Consider a 'NotificationService' responsible for sending notifications. Which option violates DIP?
The 'NotificationService' accepts an 'INotificationChannel' in its constructor.
The 'NotificationService' directly instantiates and uses an 'EmailSender' class.
The 'NotificationService' depends on an 'INotificationChannel' interface.
Different channels like 'SMSChannel' implement the 'INotificationChannel' interface.
Which of the following scenarios most likely violates the Liskov Substitution Principle?
A subclass throws an exception that is not declared in the superclass's method signature.
A subclass implements an interface that the superclass does not implement.
A subclass has more attributes than its superclass.
A subclass overrides a method of its superclass and provides additional functionality.