Angular Web Framework RxJS & Observables — Questions and Answers
Question 1: What is an Observable in Angular/RxJS?
- A lazy collection of values that can emit multiple items over time and be subscribed to (Correct answer)
- A visual component for displaying data
- A database query result set
- A CSS animation sequence
Correct answer: A lazy collection of values that can emit multiple items over time and be subscribed to
An Observable represents a stream of values over time, producing data only when subscribed to (lazy evaluation), and can emit zero or more values before completing or erroring.
Question 2: What is the difference between a Subject and an Observable?
- A Subject is both an Observable and an Observer — it can emit values and be subscribed to (Correct answer)
- They are identical in every way
- Subjects can only emit one value
- Observables can push values while Subjects cannot
Correct answer: A Subject is both an Observable and an Observer — it can emit values and be subscribed to
A Subject is multicast (multiple subscribers), can manually emit values using next(), and acts as both an Observable (can be subscribed to) and an Observer (can receive values).
Question 3: What does the pipe() method do in RxJS?
- Chains multiple operators together to transform, filter, or combine Observable streams (Correct answer)
- Sends data through a physical pipe system
- Connects to a Unix pipe for command-line operations
- Creates a new subscription to an Observable
Correct answer: Chains multiple operators together to transform, filter, or combine Observable streams
The pipe() method allows composing multiple RxJS operators (map, filter, switchMap, etc.) into a processing pipeline that transforms the Observable stream.
Question 4: What is the switchMap operator used for?
- To cancel the previous inner Observable and subscribe to a new one when the source emits (Correct answer)
- To switch between different programming languages
- To map values from one data type to another
- To toggle a boolean value in the stream
Correct answer: To cancel the previous inner Observable and subscribe to a new one when the source emits
switchMap maps each source value to an inner Observable, automatically unsubscribing from the previous inner Observable when a new source value arrives, preventing stale requests.
Question 5: Why is it important to unsubscribe from Observables?
- To prevent memory leaks by cleaning up subscriptions that are no longer needed (Correct answer)
- Unsubscribing is never necessary in Angular
- To improve the speed of the Observable
- To reset the Observable to its initial value
Correct answer: To prevent memory leaks by cleaning up subscriptions that are no longer needed
Failing to unsubscribe from Observables (especially those that don't complete naturally) creates memory leaks, as the subscription continues holding references and processing emissions.
Question 6: What is the async pipe in Angular templates?
- A pipe that automatically subscribes to an Observable, displays the latest value, and unsubscribes on destroy (Correct answer)
- A pipe for asynchronous CSS rendering
- A pipe for processing files uploaded asynchronously
- A pipe for connecting to async database queries
Correct answer: A pipe that automatically subscribes to an Observable, displays the latest value, and unsubscribes on destroy
The async pipe subscribes to an Observable or Promise in the template, renders the latest emitted value, and automatically unsubscribes when the component is destroyed, preventing memory leaks.
What is an Observable in Angular/RxJS?