Angular MCQs – Services and Dependency Injection

11.) When should you use hierarchical injectors in Angular?

A) When services should have different instances in different modules
B) When you want a service to be globally available
C) When injecting third-party libraries
D) When using @Component decorators

Answer: Option A

Explanation: Hierarchical injectors allow services to have different instances in separate modules, ensuring module-specific data handling.

12.) What happens if a service is provided in both root and a module?

A) Angular throws an error
B) It affects all other services
C) The global instance is used
D) A new instance is created for the module

Answer: Option D

Explanation: Services provided in a module create a separate instance for that module.

13.) How can a service be limited to a specific module?

A) Using { providedIn: ‘root’ }
B) Adding it in providers array of a module
C) Declaring it inside main.ts
D) Using @Component({ providers: [MyService] })

Answer: Option B

Explanation: Services limited to a module are declared in its providers array.

14.) What is an Angular Injector?

A) A tool to create components
B) A built-in function to modify services
C) A system that maintains dependencies and injects them where needed
D) A method to call services

Answer: Option C

Explanation: The Angular Injector manages dependency instances and ensures efficient service sharing.

15.) How do you prevent multiple instances of a service?

A) Use { providedIn: ‘root’ }
B) Create multiple service classes
C) Avoid using services
D) Manually create service instances

Answer: Option A

Explanation: Providing a service in root ensures it is singleton and shared across the application.

16.) Which lifecycle hook is best for initializing a service in a component?

A) ngAfterContentInit()
B) ngAfterViewInit()
C) ngOnDestroy()
D) ngOnInit()

Answer: Option D

Explanation: The ngOnInit() lifecycle hook is best for initializing a service in a component as it runs after the constructor and ensures all required dependencies are available.

17.) How can a service be injected at the component level instead of globally?

A) Declare it in @NgModule.providers
B) Use { providedIn: ‘root’ }
C) Add it in the providers array of @Component
D) Import it in app.module.ts

Answer: Option C

Explanation: Declaring a service in the providers array of a component ensures it is available only to that component and its children.

18.) What is the role of an Injector in Angular?

A) It handles the service creation and dependency injection
B) It compiles Angular components
C) It renders templates dynamically
D) It is responsible for module configuration

Answer: Option A

Explanation: The Angular Injector manages dependencies and ensures they are injected where needed.

19.) What will happen if you manually create a new service instance inside a component?

A) It optimizes performance
B) It creates multiple instances, breaking singleton behavior
C) It improves service sharing
D) It has no impact

Answer: Option B

Explanation: Manually creating a service instance breaks the singleton pattern, leading to inconsistencies and unexpected behavior.

20.) What is an alternative way to inject dependencies manually?

A) Using Injector.get(MyService)
B) Using import { MyService }
C) Using new MyService()
D) By modifying the component template

Answer: Option A

Explanation: The Injector API allows manual dependency injection when automatic DI is not an option.

Leave a Reply

Your email address will not be published. Required fields are marked *