Angular Web Framework RxJS & Observables 5 — Questions and Answers
Question 1: What does the `scan` operator do in RxJS?
- Filters values based on a cumulative condition
- Accumulates values over time like Array.reduce and emits each intermediate result (Correct answer)
- Groups emissions into fixed-size arrays
- Samples the stream at a regular interval
Correct answer: Accumulates values over time like Array.reduce and emits each intermediate result
scan applies an accumulator function to each emission and emits the running total, similar to reduce but streaming intermediate results.
Question 2: Which Angular service uses Observables under the hood to expose route parameter changes in a component?
- HttpClient
- ActivatedRoute (Correct answer)
- Router
- NgZone
Correct answer: ActivatedRoute
ActivatedRoute exposes params, queryParams, and data as Observables so components can react to navigation changes without re-creating the component.
Question 3: What happens when you call `complete()` on a Subject?
- All current subscribers receive an error notification
- The Subject emits one final undefined value then terminates
- All subscribers receive the completion notification and no further values can be emitted (Correct answer)
- The Subject resets to its initial state
Correct answer: All subscribers receive the completion notification and no further values can be emitted
Calling complete() sends the completion signal to all current subscribers and prevents any future next() or error() calls from being delivered.
Question 4: Which operator should you use to emit a default value if the source Observable completes without emitting anything?
- defaultIfEmpty (Correct answer)
- startWith
- isEmpty
- iif
Correct answer: defaultIfEmpty
defaultIfEmpty emits a specified fallback value when the source completes without ever emitting, then completes itself.
Question 5: In RxJS, what does `zip` emit compared to `combineLatest`?
- zip emits on every emission; combineLatest waits for all to complete
- zip pairs emissions by index and only emits when all sources have a new value at that index; combineLatest emits on any new value (Correct answer)
- zip multicasts; combineLatest unicasts
- zip drops slower source values; combineLatest buffers them
Correct answer: zip pairs emissions by index and only emits when all sources have a new value at that index; combineLatest emits on any new value
zip combines the nth emission from each source into one array and waits for all sources to provide their nth value before emitting.
Question 6: What is the purpose of the `distinctUntilChanged` operator?
- Removes all duplicate values from the entire stream history
- Suppresses consecutive duplicate emissions, only emitting when the value changes (Correct answer)
- Sorts stream values and removes duplicates globally
- Throttles emissions so the same value cannot be emitted twice per second
Correct answer: Suppresses consecutive duplicate emissions, only emitting when the value changes
distinctUntilChanged compares each emission to the immediately preceding one and skips the emission if they are equal.
Question 7: Which RxJS operator is used to perform side effects (like logging) without modifying the emitted values?
- map
- tap (Correct answer)
- do
- peek
Correct answer: tap
tap (formerly named do) executes a side-effect function for each emission while passing the values through unchanged.
What does the `scan` operator do in RxJS?