Bird
0
0

Given this Angular component code:

medium📝 Predict Output Q4 of 15
Angular - RxJS and Observables Fundamentals
Given this Angular component code:
export class SampleComponent implements OnInit, OnDestroy {
  data$ = this.service.getData();
  subscription: Subscription | undefined;

  ngOnInit() {
    this.subscription = this.data$.subscribe(value => console.log(value));
  }

  ngOnDestroy() {
    this.subscription?.unsubscribe();
  }
}

What happens if ngOnDestroy does not unsubscribe?
AThe subscription continues, causing potential memory leaks
BThe subscription automatically stops when component is destroyed
CThe Observable stops emitting values globally
DAngular throws an error preventing component destruction
Step-by-Step Solution
Solution:
  1. Step 1: Understand subscription lifecycle

    Subscriptions remain active until explicitly unsubscribed.
  2. Step 2: Effect of missing unsubscribe in ngOnDestroy

    Without unsubscribe, the subscription stays active even after component removal, causing memory leaks.
  3. Final Answer:

    The subscription continues, causing potential memory leaks -> Option A
  4. Quick Check:

    Missing unsubscribe effect = C [OK]
Quick Trick: Always unsubscribe to stop active subscriptions on destroy [OK]
Common Mistakes:
MISTAKES
  • Assuming Angular auto-unsubscribes
  • Thinking Observable stops globally
  • Expecting errors on missing unsubscribe

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes