Sample questions
Question 1Easy
What must you do to receive values from a cold Observable?
- Subscribe to it
- Call it like a function
- Await it directly
- Import it into a template only
Answer: A. Subscribe to it
Explanation
Observables are lazy; nothing runs until subscribe() is called.
Step-by-step solution
- Cold observables do not start running until they are subscribed to.
- You must call subscribe() on the observable to begin receiving values.
- Subscribe is the method used to start an observable and get values from it.
- So, you must subscribe to it to receive values.
- Final answer: A (Subscribe to it).
Why the other options are wrong
- B. Calling an observable like a function is not valid; you subscribe to receive its values.
- C. Await does not work on a plain observable; you subscribe or use the async pipe.
- D. Importing into a template does not produce values; the observable must be subscribed.
Tip: Remember: Always subscribe to cold observables to get values.
Question 2Easy
Which method must you call on an Observable to start receiving values?
- start() to begin the stream
- subscribe()
- run() to execute the pipeline
- emit() to push a value
Answer: B. subscribe()
Explanation
Observables are lazy until subscribed.
Step-by-step solution
- Lazy streams.
Why the other options are wrong
- A. Observables have no start method; subscribe is what starts them.
- C. Observables have no run method; subscribing starts the stream.
- D. Subjects emit with next().
Question 3Easy
Which operator transforms each emitted value?
- tap
- take
- filter
- map
Answer: D. map
Explanation
map projects each value through a function.
Step-by-step solution
- Transform.
Why the other options are wrong
- A. tap runs side-effects.
- B. take limits how many values pass; it does not transform them.
- C. filter drops values by condition; it does not transform them.
Question 4Easy
What does of(1, 2, 3) create?
- A promise that resolves to 1, 2, 3
- An array
- An observable that emits 1, 2, 3 then completes
- A subject that waits for next() calls
Answer: C. An observable that emits 1, 2, 3 then completes
Explanation
of emits arguments synchronously.
Step-by-step solution
- Creation function.
Why the other options are wrong
- A. of creates an observable, not a promise.
- B. of returns an observable; it does not create an array.
- D. of creates a cold observable, not a subject waiting for next.
Question 5Medium
Which operator takes only the first n values then completes?
- skip(n)
- last(n)
- first(n) always
- take(n)
Answer: D. take(n)
Explanation
take completes after n emissions.
Step-by-step solution
- Limit.
Why the other options are wrong
- A. skip drops the first n values; it does not take them.
- B. last is not an RxJS operator for taking the first n values.
- C. first takes one value and has no n argument.
Question 6Medium
Why unsubscribe from long-lived observables in ngOnDestroy?
- To make the template compile
- To reset the router state after navigation
- To avoid memory leaks and callbacks on destroyed components
- To save HTTP bandwidth on repeated calls
Answer: C. To avoid memory leaks and callbacks on destroyed components
Explanation
Subscriptions keep references alive.
Step-by-step solution
- Clean up.
Why the other options are wrong
- A. Unsubscribing has nothing to do with template compilation.
- B. Unsubscribing does not reset router state after navigation.
- D. Unsubscribing does not save bandwidth on repeated HTTP calls.
Tip: async pipe unsubscribes for you.
Question 7Medium
What is the difference between Subject and BehaviorSubject?
- Subject is synchronous only, while BehaviorSubject is asynchronous
- BehaviorSubject holds a current value and replays it to new subscribers
- BehaviorSubject cannot emit new values after creation
- They are the same
Answer: B. BehaviorSubject holds a current value and replays it to new subscribers
Explanation
BehaviorSubject needs an initial value.
Step-by-step solution
- New subscribers get latest.
Why the other options are wrong
- A. Both are synchronous; the difference is replaying the latest value.
- C. BehaviorSubject can emit new values like any other subject.
- D. They differ: BehaviorSubject stores and replays its current value.
Question 8Medium
What will this print?
of(1, 2, 3, 4).pipe(filter(n => n % 2 === 0), map(n => n * 10)).subscribe(console.log)
- 10 then 30
- 20 then 40 then 60 then 80
- 2 then 4
- 20 then 40
Answer: D. 20 then 40
Explanation
filter keeps 2 and 4, then map multiplies them by 10, giving 20 and 40.
Step-by-step solution
- Filter keeps even numbers: 2 and 4.
- Map multiplies by 10.
- Output 20, 40.
Why the other options are wrong
- A. That maps the odd numbers.
- B. That prints four values, but filter removes 1 and 3, so only two values are printed.
- C. That skips the map step.
Question 9Hard
Which flattening operator ignores new source values while an inner observable is still running, useful for preventing duplicate submit clicks?
- switchMap
- concatMap
- mergeMap
- exhaustMap
Answer: D. exhaustMap
Explanation
exhaustMap drops outer values until the current inner observable completes.
Step-by-step solution
- ExhaustMap ignores new source values while an inner observable is running.
- It ensures only one inner observable runs at a time.
- This prevents duplicate submit clicks by blocking new requests.
- The inner observable completes before allowing the next source value.
- Thus, exhaustMap is ideal for preventing duplicate submits.
- Final answer: exhaustMap.
Why the other options are wrong
- A. switchMap cancels the previous inner observable when a new value arrives, rather than ignoring new ones.
- B. concatMap queues inner observables one after another, so it does not ignore new source values.
- C. mergeMap runs inner observables in parallel, so it does not ignore new source values.
Tip: Use exhaustMap to block new requests while an existing one is still processing.
Question 10Hard
What is a common way to avoid memory leaks from long-lived subscriptions in a component?
- Subscribe inside the constructor only, because later subscriptions are not cleaned
- Wrap every subscribe call in try/catch so that errors never reach the zone
- Use the async pipe or a takeUntil style operator and unsubscribe in ngOnDestroy
- Log values with console.log inside subscribe and then reload the page
Answer: C. Use the async pipe or a takeUntil style operator and unsubscribe in ngOnDestroy
Explanation
Unfinished subscriptions keep references alive; auto or manual teardown on destroy prevents leaks.
Step-by-step solution
- Subscriptions not unsubscribed cause memory leaks.
- Async pipe or takeUntil operator handle cleanup automatically.
- Unsubscribe in ngOnDestroy ensures no lingering references.
- This pattern avoids leaks in long-lived subscriptions.
- Final answer: Use the async pipe or a takeUntil style operator and unsubscribe in ngOnDestroy.
Why the other options are wrong
- A. Subscribing only in the constructor is not the fix; leaks come from not cleaning up subscriptions.
- B. try/catch does not stop a subscription from leaking; it only handles errors thrown during subscribe.
- D. console.log and a page reload do not clean up subscriptions and do not prevent leaks.
Tip: Always unsubscribe in ngOnDestroy to prevent memory leaks.
Practise all 21 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →