Bird
0
0

Given this service code, what will the console log after calling increment() twice?

medium📝 component behavior Q13 of 15
Angular - State Management
Given this service code, what will the console log after calling increment() twice?
import { Injectable, signal } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class CounterService {
  count = signal(0);

  increment() {
    this.count.update(c => c + 1);
  }
}

const service = new CounterService();
service.increment();
service.increment();
console.log(service.count());
A1
B0
C2
Dundefined
Step-by-Step Solution
Solution:
  1. Step 1: Understand initial signal value

    The signal count starts at 0.
  2. Step 2: Apply two increments

    Each increment adds 1, so after two calls, count is 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    0 + 1 + 1 = 2 [OK]
Quick Trick: Each update adds 1; two calls add 2 total [OK]
Common Mistakes:
  • Forgetting to call the signal as a function to get value
  • Assuming count resets after each increment
  • Confusing update with set method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes