0
0
Angularframework~20 mins

Singleton service behavior in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Singleton Service Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Angular ensure a service is a singleton?

Consider an Angular service provided in the root injector. What happens when multiple components inject this service?

AAll components receive the same instance of the service.
BEach component gets a new instance of the service.
COnly the first component gets the service instance; others get null.
DAngular throws an error if multiple components inject the same service.
Attempts:
2 left
💡 Hint

Think about how Angular's root injector works for services provided in 'root'.

state_output
intermediate
2:00remaining
What is the value of the counter after multiple component injections?

Given a singleton service with a counter property initialized to 0, two components increment it by 1 each. What is the final counter value?

Angular
export class CounterService {
  counter = 0;
  increment() { this.counter++; }
}

// Component A and Component B both inject CounterService and call increment() once.
AError: counter is undefined
B1
C0
D2
Attempts:
2 left
💡 Hint

Remember the service is a singleton shared by both components.

🔧 Debug
advanced
2:00remaining
Why does providing a service in a component break singleton behavior?

What happens if you provide a service in the providers array of two different components instead of in root?

AAngular still shares one instance across components.
BAngular throws a runtime error about multiple providers.
CEach component gets its own separate instance of the service.
DThe service instance is shared only if components are siblings.
Attempts:
2 left
💡 Hint

Think about how Angular creates injectors for components with providers.

🧠 Conceptual
advanced
2:00remaining
How does Angular's injector hierarchy affect singleton services?

Which statement best describes how Angular's hierarchical injectors influence singleton service instances?

AA service provided in root is shared app-wide unless overridden in a child injector.
BServices provided in root are recreated for each lazy-loaded module automatically.
CSingleton services cannot be overridden in child injectors.
DAngular merges all injectors into one global injector at runtime.
Attempts:
2 left
💡 Hint

Consider how Angular allows overriding services in feature modules or components.

📝 Syntax
expert
3:00remaining
Which code snippet correctly provides a singleton service in Angular 17+ standalone component?

Choose the correct way to provide a singleton service in a standalone Angular component using Angular 17+ syntax.

A
import { inject } from '@angular/core';

@Component({
  standalone: true,
  providers: [MyService]
})
export class MyComponent {
  service = inject(MyService);
}
B
@Component({
  standalone: true
})
export class MyComponent {
  constructor(private service: MyService) {}
}

@Injectable({providedIn: 'root'})
export class MyService {}
C
@Component({
  standalone: true,
  providers: [{provide: MyService, useClass: MyService}]
})
export class MyComponent {
  service = inject(MyService);
}

@Injectable()
export class MyService {}
D
@Component({
  standalone: true
})
export class MyComponent {
  service = new MyService();
}

@Injectable({providedIn: 'root'})
export class MyService {}
Attempts:
2 left
💡 Hint

Remember that providing a service in 'root' makes it singleton app-wide, and standalone components can inject it via constructor.