Angular Web Framework RxJS & Observables 4 — Questions and Answers
Question 1: What is the correct way to multicast an Observable to multiple subscribers using a single execution in RxJS?
- Wrap it in a new Observable using new Observable()
- Use the share() operator (Correct answer)
- Call subscribe() multiple times on the same Observable
- Convert it to a Promise with toPromise()
Correct answer: Use the share() operator
share() wraps the source in a Subject and multicasts it so all subscribers share one underlying execution rather than triggering separate ones.
Question 2: Which operator is best suited for sequential HTTP requests where the order of results must match the order of source emissions?
- mergeMap
- switchMap
- concatMap (Correct answer)
- exhaustMap
Correct answer: concatMap
concatMap subscribes to each inner Observable only after the previous one completes, preserving order at the cost of parallelism.
Question 3: What does `BehaviorSubject` require that a regular `Subject` does not?
- An error handler on construction
- An initial value provided at construction time (Correct answer)
- At least one subscriber before emitting
- A completion callback
Correct answer: An initial value provided at construction time
BehaviorSubject must be initialized with a seed value and always emits the most recent value to new subscribers immediately upon subscription.
Question 4: Which RxJS operator emits only the first value that passes a predicate, then completes?
- filter
- first (Correct answer)
- take
- find
Correct answer: first
first() emits the first value matching an optional predicate and immediately completes the stream.
Question 5: How does `mergeMap` differ from `concatMap` when handling multiple concurrent inner Observables?
- mergeMap processes them sequentially; concatMap processes them in parallel
- mergeMap subscribes to all inner Observables simultaneously; concatMap queues them (Correct answer)
- mergeMap cancels previous inner Observables; concatMap retains them all
- mergeMap only accepts synchronous sources; concatMap accepts async sources
Correct answer: mergeMap subscribes to all inner Observables simultaneously; concatMap queues them
mergeMap subscribes to every inner Observable immediately without waiting, allowing concurrent execution unlike concatMap's sequential approach.
Question 6: What is the difference between `Subject` and `Observable` in RxJS?
- Subject cannot emit values; Observable can
- Subject is both an Observable and an Observer, allowing external code to push values (Correct answer)
- Observable supports multicasting natively; Subject does not
- Subject is lazy; Observable is eager
Correct answer: Subject is both an Observable and an Observer, allowing external code to push values
A Subject implements both the Observable and Observer interfaces, so external code can call next(), error(), or complete() on it directly.
Question 7: Which operator converts a higher-order Observable (Observable of Observables) into a first-order Observable using merge strategy?
- concatAll
- mergeAll (Correct answer)
- switchAll
- exhaustAll
Correct answer: mergeAll
mergeAll subscribes to each inner Observable as it arrives and merges their emissions concurrently into a single stream.
What is the correct way to multicast an Observable to multiple subscribers using a single execution in RxJS?