WorksheetsMastering Interfaces and Dependency Injection
Total questions: 20
Worksheet time: 7mins
What is an interface in object-oriented programming?
Contract that defines methods for classes to implement.
Type of class that cannot be instantiated.
Method that performs a specific function.
Collection of variables and constants only.
How do interfaces promote code reusability?
Allowing multiple classes to implement the same methods, enabling interchangeable use of different types.
Eliminate the need for code documentation.
Restrict the use of methods to specific classes.
Can only be used by a single class.
Explain the concept of Dependency Injection (DI).
Technique for managing database connections.
Method for creating global variables.
Design pattern that allows a class to receive its dependencies from an external source rather than creating them internally.
Way to optimize code performance by reducing memory usage.
What are the benefits of using Dependency Injection?
Improved testability, maintainability, readability, and separation of concerns.
Reduced performance due to overhead
Tighter coupling between components
Increased complexity in code management
What is the Interface Segregation Principle?
Suggests combining multiple interfaces into one for simplicity.
Advocates for creating smaller, specific interfaces to avoid forcing clients to depend on unused methods.
Focuses on using a single large interface for better performance.
States that all clients must implement all methods of an interface.
How does the Interface Segregation Principle improve code design?
Improves code design by promoting smaller, specific interfaces that reduce dependencies and enhance modularity.
It focuses on reducing the number of classes in a system.
It encourages large, complex interfaces that increase dependencies.
It promotes the use of global interfaces for all classes.
Give an example of a violation of the Interface Segregation Principle.
A class that implements an interface with methods that are all needed.
A class that uses multiple interfaces but does not implement any methods.
A class implementing a large interface with both print() and scan() methods, but only needing print().
A class implementing a small interface with only print() method.
What is the Dependency Inversion Principle?
High-level modules should not depend on low-level modules; both should depend on abstractions.
Abstractions should depend on concrete implementations.
Modules should be tightly coupled for better performance.
High-level modules should depend on low-level modules directly.
How does Dependency Inversion differ from traditional dependency management?
Dependency Inversion is only applicable to high-level modules.
Traditional dependency management encourages loose coupling between modules.
Dependency Inversion eliminates the need for abstractions entirely.
Reduce coupling, while traditional dependency management often leads to tight coupling between high-level and low-level modules.
How can Dependency Injection lead to better testability?
Allowing easy substitution of dependencies with mocks or stubs, enabling isolated and controlled testing.
Dependency Injection reduces the flexibility of code, making it less testable.
Dependency Injection complicates testing by making it harder to manage dependencies.
Dependency Injection eliminates the need for testing altogether.
What is the role of a service locator in Dependency Injection?
To handle user authentication and authorization.
To log application errors and exceptions.
To manage database connections in an application.
To provide a centralized registry for resolving service dependencies.
Explain how constructor injection works.
Constructor injection allows dependencies to be accessed globally.
Constructor injection uses setter methods to provide dependencies.
Constructor injection is a method for creating static classes.
Constructor injection provides dependencies to a class via its constructor parameters.
What is method injection in Dependency Injection?
Using global variables for dependencies.
Process of creating dependencies at compile time.
Practice of passing dependencies directly to a method at runtime.
Technique for storing dependencies in a database.
What are the consequences of not following the Interface Segregation Principle?
Simplified code structure
Improved performance and efficiency
Increased coupling, reduced maintainability, and difficulties in implementing changes.
Enhanced user experience
How can Dependency Injection help in managing application complexity?
Increases application size.
Makes code harder to understand.
Eliminates the need for testing.
Promoting loose coupling, enhancing testability, and simplifying code maintenance.
Can a class implement multiple interfaces
Yes
No
Only if they don’t conflict
Only with inheritance
What happens if a class does not implement all methods of its interface?
The compiler throws an error
Which SOLID principle encourages using smaller, focused interfaces?
In ASP.NET Core, where do you register services for dependency injection?
Web.config
Program.cs
Controller.cs
Which of the following is not a valid service lifetime in .NET Core DI?
AddSingleton
AddPersistent
AddScoped
AddTransient
