Sample questions
Question 1Easy
How does Angular typically give a component a service?
- By calling new Service() in the template
- By importing the file only
- Through constructor injection by type
- By a global variable
Answer: C. Through constructor injection by type
Explanation
The injector resolves constructor parameter types.
Step-by-step solution
- Declare a parameter.
- Injector supplies it.
Why the other options are wrong
- A. Templates cannot create services with new; Angular injects them.
- B. Import is not instance creation.
- D. Angular injects services by type, not through global variables.
Question 2Easy
Which decorator marks a class as available for dependency injection?
- @Injectable
- @Component only
- @Inject only
- @Pipe
Answer: A. @Injectable
Explanation
@Injectable allows Angular's injector to create the class and supply its own dependencies.
Step-by-step solution
- @Injectable marks a class for dependency injection in Angular.
- It tells Angular's DI system to manage the class instance.
- Other decorators like @Component or @Inject are not used for this purpose.
- The correct decorator is @Injectable.
- Final answer: A (@Injectable).
Why the other options are wrong
- B. @Component marks a component, not a class for injection; @Injectable is the decorator for services.
- C. @Inject picks a token at a constructor parameter; it does not mark a class as injectable.
- D. @Pipe marks a template pipe; it does not make a class available for dependency injection.
Tip: Remember: @Injectable is needed for classes to be injected in Angular.
Question 3Easy
What does @Injectable({providedIn: 'root'}) do?
- Creates a new instance in every component that asks for the service
- Registers the service as a singleton available app-wide
- Restricts the service to one module
- Marks a class as a component
Answer: B. Registers the service as a singleton available app-wide
Explanation
Root providers create one shared instance.
Step-by-step solution
- Root = application-level injector.
Why the other options are wrong
- A. That needs component providers.
- C. providedIn root makes the service app-wide, not limited to one module.
- D. Components use @Component.
Question 4Easy
Why are services commonly used in Angular?
- To render templates and handle CSS for the whole application
- To define the HTML structure of every page in the application
- To parse URLs and replace the Router module entirely
- To share data and logic across components
Answer: D. To share data and logic across components
Explanation
Services hold reusable logic and state that many components can access through DI.
Step-by-step solution
- Services in Angular allow multiple components to use the same data and functions.
- This avoids code duplication and makes the application more maintainable.
- Components can inject services to access shared logic or state easily.
- Sharing data and logic across components is a key reason for using services.
- The correct answer is To share data and logic across components.
Why the other options are wrong
- A. Rendering templates and CSS is the framework's job, not the reason services are used.
- B. Services do not define HTML structure; templates do that in components.
- C. Services do not parse URLs or replace the Router; routing is a separate module.
Tip: Remember: Services help share logic and data between components in Angular.
Question 5Medium
What does providedIn: 'root' in @Injectable do?
- A new instance for every component that injects it, on creation
- A single app-wide instance in the root injector, tree-shakable
- Restricts the service to the AppComponent and its direct children only
- Makes the service lazy-loaded and re-created on each route navigation
Answer: B. A single app-wide instance in the root injector, tree-shakable
Explanation
With providedIn root the service is a singleton in the root injector and can be removed from the bundle if never injected.
Step-by-step solution
- ProvidedIn: 'root' makes the service available in the root injector.
- Root injector creates a single instance of the service for the entire app.
- This instance is shared across all components that inject it.
- The service can be tree-shaken if not used, reducing bundle size.
- So, providedIn: 'root' provides a singleton in the root injector, tree-shakable.
Why the other options are wrong
- A. A new instance per component is what component-level providers do, not providedIn root.
- C. Restricting to AppComponent is not what providedIn root does; it is app-wide in the root injector.
- D. providedIn root does not lazy-load or re-create the service on each route navigation.
Tip: Use providedIn: 'root' for global services that need to be a singleton.
Question 6Medium
What does @Optional() on a constructor parameter do?
- Makes the service lazy so that it is created only when first used
- Hides the parameter from the template
- Injects null instead of throwing when no provider is found
- Skips change detection
Answer: C. Injects null instead of throwing when no provider is found
Explanation
Without it a missing provider causes an error.
Step-by-step solution
- Missing provider -> null.
Why the other options are wrong
- A. Optional does not make a service lazy; it allows a missing provider.
- B. Optional affects injection, not the template visibility of a parameter.
- D. Optional concerns dependency resolution, not change detection.
Question 7Medium
What does providing a service in a component's providers array do?
- Makes it a global singleton shared by every component in the whole application
- Disables injection for the component
- Stores it in localStorage for later
- Creates a separate instance for that component and its children
Answer: D. Creates a separate instance for that component and its children
Explanation
Component-level providers scope the instance to the component subtree.
Step-by-step solution
- Providers on component create a scoped injector.
Why the other options are wrong
- A. A component providers array scopes the instance; it is not global.
- B. Providers supply instances; they do not disable injection.
- C. Providers create service instances; they do not persist data to storage.
Question 8Medium
What does the inject() function allow that constructor injection does not?
- Injecting without any providers being registered anywhere in the app
- Injecting dependencies after the component has already been destroyed
- Getting dependencies inside field initialisers and functions in an injection context
- Overriding the root injector at runtime with a different instance
Answer: C. Getting dependencies inside field initialisers and functions in an injection context
Explanation
inject() works in an injection context, such as field initialisers, factory functions and guards.
Step-by-step solution
- Constructor injection works only where the constructor runs.
- inject() works in an injection context, such as field initialisers and factory functions.
- So inject() can get dependencies in fields and functions as well. The correct answer is C.
Why the other options are wrong
- A. Providers are still needed; inject() does not skip them.
- B. A destroyed component cannot receive new injections.
- D. inject() does not replace the root injector at runtime.
Tip: Use inject() in field initialisers and functions that run inside an injection context.
Question 9Hard
Two components each declare providers: [CounterService]. Component A increments count to 3. What does component B see?
- 3, because the instance is shared
- undefined, because the service is not provided
- An injection error because of duplicate providers
- 0, because each has its own instance
Answer: D. 0, because each has its own instance
Explanation
Component providers create independent instances.
Step-by-step solution
- Separate providers, separate instances.
Why the other options are wrong
- A. Shared only with root.
- B. Each component that provides the service gets its own created instance.
- C. Providing the same service in two components is valid and raises no error.
Question 10Hard
What is the purpose of an InjectionToken?
- To inject values that have no class type, such as config objects
- To store authentication tokens (JWTs) securely in browser storage
- To encrypt injected services so that other classes cannot read them
- To lazy-load modules once a user triggers a specific route in the app
Answer: A. To inject values that have no class type, such as config objects
Explanation
InjectionToken creates a unique DI key for non-class values; use @Inject(TOKEN) or inject(TOKEN).
Step-by-step solution
- InjectionToken creates a unique identifier for non-class values like config objects.
- It allows injecting these values into other classes using @Inject(TOKEN) or inject(TOKEN).
- This makes it possible to pass data without creating a class for it.
- Other options describe unrelated features like JWT storage or encryption.
- The correct use is for non-class values such as config objects.
Why the other options are wrong
- B. Storing JWTs in browser storage is not what InjectionToken does; it names non-class values.
- C. InjectionToken does not encrypt services; it provides a key for injecting values.
- D. Lazy-loading modules is done by router loadChildren, not by InjectionToken.
Tip: Use InjectionToken for non-class values like configs, not for JWTs or encryption.
Practise all 17 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →