Angular Web Framework Services & Dependency Injection — Questions and Answers
Question 1: What is a service in Angular?
- A class with a focused purpose that provides shared functionality across components (Correct answer)
- A background process that runs on the server
- An HTML element for displaying content
- A CSS animation keyframe
Correct answer: A class with a focused purpose that provides shared functionality across components
Angular services are TypeScript classes decorated with @Injectable() that encapsulate reusable business logic, data access, or utility functions that can be shared across multiple components.
Question 2: What is dependency injection (DI) in Angular?
- A design pattern where dependencies are provided to a class rather than created by it (Correct answer)
- A method for injecting malicious code into applications
- A database query optimization technique
- A CSS injection for dynamic styling
Correct answer: A design pattern where dependencies are provided to a class rather than created by it
Angular's DI system creates and provides instances of services to components and other services that declare dependencies, promoting loose coupling and testability.
Question 3: What does 'providedIn: root' mean in an @Injectable() decorator?
- The service is available application-wide as a singleton through the root injector (Correct answer)
- The service is only available in the root component
- The service requires root/admin permissions
- The service stores data in the root directory
Correct answer: The service is available application-wide as a singleton through the root injector
'providedIn: root' registers the service with the root injector, making a single instance available throughout the entire application and enabling tree-shaking of unused services.
Question 4: What is the purpose of HttpClient in Angular?
- To make HTTP requests to APIs and external services (Correct answer)
- To create HTML client-side templates
- To manage client-side routing
- To handle WebSocket connections only
Correct answer: To make HTTP requests to APIs and external services
HttpClient is Angular's built-in service for making HTTP requests (GET, POST, PUT, DELETE) to backend APIs, returning Observables that can be subscribed to for response handling.
Question 5: What is an interceptor in Angular?
- A service that intercepts and optionally modifies HTTP requests or responses globally (Correct answer)
- A debugging tool that intercepts console logs
- A routing guard that intercepts navigation
- An error handler that intercepts all exceptions
Correct answer: A service that intercepts and optionally modifies HTTP requests or responses globally
HTTP interceptors sit between the application and the HTTP client, allowing global modification of outgoing requests (adding auth tokens) or incoming responses (error handling).
Question 6: What is the difference between a service and a component in Angular?
- Components manage UI and views; services contain reusable business logic and data access (Correct answer)
- There is no difference
- Services render HTML; components do not
- Components are only used on the server side
Correct answer: Components manage UI and views; services contain reusable business logic and data access
Components are responsible for presenting the user interface and handling user interactions, while services encapsulate reusable logic, data access, and cross-cutting concerns shared across components.
What is a service in Angular?