Bird
0
0

Examine this Angular component:

medium📝 Predict Output Q4 of 15
Angular - RxJS and Observables Fundamentals
Examine this Angular component:
export class SampleComponent implements OnDestroy {
  private sub = this.api.getData().subscribe();

  ngOnDestroy() {
    // Missing unsubscribe here
  }
}

What is the likely consequence if sub.unsubscribe() is not called in ngOnDestroy?
AAngular will automatically unsubscribe, so no issues will occur
BThe subscription will continue running, potentially causing memory leaks
CThe component will throw a runtime error on destruction
DThe subscription will pause until the component is recreated
Step-by-Step Solution
Solution:
  1. Step 1: Recognize subscription lifecycle

    Subscriptions remain active unless explicitly unsubscribed.
  2. Step 2: Understand Angular's behavior

    Angular does not auto-unsubscribe for most Observables.
  3. Step 3: Consequence of missing unsubscribe

    Active subscriptions after component destruction cause memory leaks and unexpected side effects.
  4. Final Answer:

    The subscription will continue running, potentially causing memory leaks -> Option B
  5. Quick Check:

    Missing unsubscribe leads to lingering subscriptions [OK]
Quick Trick: Unsubscribe in ngOnDestroy to avoid leaks [OK]
Common Mistakes:
MISTAKES
  • Assuming Angular cleans up subscriptions automatically
  • Expecting runtime errors when unsubscribing is missed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes