RxJS and Observables Questions with Answers

21 practice questions on RxJS and Observables (Angular) with answers and step-by-step solutions. Try 10 samples below, then practise all 21 free in the AptiNest app.

Sample questions

Question 1Easy

What must you do to receive values from a cold Observable?

  1. Subscribe to it
  2. Call it like a function
  3. Await it directly
  4. 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

  1. Cold observables do not start running until they are subscribed to.
  2. You must call subscribe() on the observable to begin receiving values.
  3. Subscribe is the method used to start an observable and get values from it.
  4. So, you must subscribe to it to receive values.
  5. 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?

  1. start() to begin the stream
  2. subscribe()
  3. run() to execute the pipeline
  4. emit() to push a value

Answer: B. subscribe()

Explanation

Observables are lazy until subscribed.

Step-by-step solution

  1. 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?

  1. tap
  2. take
  3. filter
  4. map

Answer: D. map

Explanation

map projects each value through a function.

Step-by-step solution

  1. 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?

  1. A promise that resolves to 1, 2, 3
  2. An array
  3. An observable that emits 1, 2, 3 then completes
  4. 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

  1. 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?

  1. skip(n)
  2. last(n)
  3. first(n) always
  4. take(n)

Answer: D. take(n)

Explanation

take completes after n emissions.

Step-by-step solution

  1. 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?

  1. To make the template compile
  2. To reset the router state after navigation
  3. To avoid memory leaks and callbacks on destroyed components
  4. 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

  1. 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?

  1. Subject is synchronous only, while BehaviorSubject is asynchronous
  2. BehaviorSubject holds a current value and replays it to new subscribers
  3. BehaviorSubject cannot emit new values after creation
  4. 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

  1. 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)
  1. 10 then 30
  2. 20 then 40 then 60 then 80
  3. 2 then 4
  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

  1. Filter keeps even numbers: 2 and 4.
  2. Map multiplies by 10.
  3. 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?

  1. switchMap
  2. concatMap
  3. mergeMap
  4. exhaustMap

Answer: D. exhaustMap

Explanation

exhaustMap drops outer values until the current inner observable completes.

Step-by-step solution

  1. ExhaustMap ignores new source values while an inner observable is running.
  2. It ensures only one inner observable runs at a time.
  3. This prevents duplicate submit clicks by blocking new requests.
  4. The inner observable completes before allowing the next source value.
  5. Thus, exhaustMap is ideal for preventing duplicate submits.
  6. 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?

  1. Subscribe inside the constructor only, because later subscriptions are not cleaned
  2. Wrap every subscribe call in try/catch so that errors never reach the zone
  3. Use the async pipe or a takeUntil style operator and unsubscribe in ngOnDestroy
  4. 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

  1. Subscriptions not unsubscribed cause memory leaks.
  2. Async pipe or takeUntil operator handle cleanup automatically.
  3. Unsubscribe in ngOnDestroy ensures no lingering references.
  4. This pattern avoids leaks in long-lived subscriptions.
  5. 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 →

Related topics

See all Angular topics · All topics