Bird
0
0

Consider this code snippet:

medium📝 state output Q13 of 15
Angular - RxJS and Observables Fundamentals
Consider this code snippet:
const subject = new BehaviorSubject('start');
subject.next('first');
subject.subscribe(value => console.log('Subscriber 1:', value));
subject.next('second');
subject.subscribe(value => console.log('Subscriber 2:', value));

What will be printed to the console?
ASubscriber 1: start<br>Subscriber 1: first<br>Subscriber 1: second<br>Subscriber 2: second
BSubscriber 1: first<br>Subscriber 1: second<br>Subscriber 2: first<br>Subscriber 2: second
CSubscriber 1: first<br>Subscriber 1: second<br>Subscriber 2: second
DSubscriber 1: start<br>Subscriber 1: second<br>Subscriber 2: start<br>Subscriber 2: second
Step-by-Step Solution
Solution:
  1. Step 1: Trace BehaviorSubject emissions

    BehaviorSubject starts with 'start', then next emits 'first'. Subscriber 1 subscribes after 'first', so it receives the last value 'first' immediately, then 'second' when emitted.
  2. Step 2: Analyze Subscriber 2 subscription

    Subscriber 2 subscribes after 'second' is emitted, so it immediately receives 'second'.
  3. Final Answer:

    Subscriber 1: start
    Subscriber 1: first
    Subscriber 1: second
    Subscriber 2: second
    -> Option A
  4. Quick Check:

    BehaviorSubject sends last value on subscribe [OK]
Quick Trick: BehaviorSubject sends last value immediately on subscribe [OK]
Common Mistakes:
MISTAKES
  • Assuming Subscriber 2 gets 'first' value
  • Ignoring initial 'start' emission to Subscriber 1
  • Ignoring immediate emission on subscription

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes