Angular services and dependency injection are crucial for building scalable and maintainable applications. Services help in sharing data between components, while dependency injection (DI) ensures efficient management of dependencies.
These Multiple-choice questions (MCQs) cover key concepts such as DI, service creation, injectors, and providers, helping you build efficient, modular applications. Preparing these MCQs will boost your confidence in interviews and real-world projects.
1.) What is an Angular service?
A) A UI component that displays data
B) A reusable class used to share data or logic across components
C) A decorator used to define components
D) A built-in Angular module
2.) What decorator is used to create an Angular service?
A) @Injectable
B) @Component
C) @Directive
D) @Service
3.) What is Dependency Injection (DI) in Angular?
A) A way to define component styles
B) A technique for handling HTTP requests
C) A design pattern that manages dependencies automatically
D) A way to write unit tests
4.) Which of the following is NOT a reason to use services in Angular?
A) To store reusable logic
B) To fetch and manage data
C) To share data between components
D) To increase memory usage
5.) Where should services be provided in Angular?
A) Only in app.module.ts
B) Only in components
C) In @NgModule or @Component using providedIn
D) In main.ts
6.) What does { providedIn: ‘root’ } do in a service?
A) Limits the service to one module
B) Makes the service available globally
C) Prevents dependency injection
D) Forces Angular to create multiple instances
7.) How do you inject a service into a component?
A) Using constructor(private service: MyService) { }
B) Using import { MyService } only
C) By calling new MyService()
D) By using @Injectable({ component: ‘AppComponent’ })
8.) What is the singleton pattern in Angular services?
A) Ensuring multiple instances of a service exist
B) Limiting a service to one instance across the application
C) Creating an instance of a service for each component
D) Preventing dependency injection
9.) What happens when a service is removed from the providers array but still injected into a component?
A) Angular throws an error
B) A new instance is created manually
C) The service is automatically re-added
D) The component works fine without it
10.) How does Angular inject dependencies?
A) Using a centralized configuration file
B) By manually creating instances in components
C) Automatically using constructors and decorators
D) By hardcoding dependencies
Related