0
0
Angularframework~20 mins

ngOnDestroy for cleanup in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Cleanup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when ngOnDestroy is called in an Angular component?
Consider an Angular component that subscribes to a data stream. What is the main purpose of implementing the ngOnDestroy lifecycle hook in this component?
ATo update the component's view after data changes
BTo initialize the component's data when it is created
CTo unsubscribe from the data stream and release resources before the component is destroyed
DTo handle user input events during the component's lifetime
Attempts:
2 left
💡 Hint
Think about what happens when a component is removed from the screen and how to avoid memory leaks.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly implements ngOnDestroy to clean up a subscription?
You have a subscription stored in this.sub. Which option correctly unsubscribes in ngOnDestroy?
Angular
export class MyComponent implements OnDestroy {
  sub = this.dataService.getData().subscribe();

  ngOnDestroy() {
    // What goes here?
  }
}
Athis.sub.unsubscribe();
Bthis.sub.close();
Cthis.sub.stop();
Dthis.sub.cancel();
Attempts:
2 left
💡 Hint
Subscriptions have a method to stop receiving data.
🔧 Debug
advanced
2:00remaining
Why does this Angular component cause a memory leak?
Look at this component code. Why might it cause a memory leak when the component is destroyed?
Angular
export class LeakComponent implements OnDestroy {
  sub = this.dataService.getData().subscribe(data => {
    console.log(data);
  });

  ngOnDestroy() {
    // No cleanup here
  }
}
ABecause the subscription is never unsubscribed in ngOnDestroy
BBecause ngOnDestroy is not implemented correctly with a return type
CBecause the subscription callback logs data to the console
DBecause the component does not implement OnInit
Attempts:
2 left
💡 Hint
Think about what happens to active subscriptions when a component is removed.
🧠 Conceptual
advanced
2:00remaining
What is the best practice for cleaning up multiple subscriptions in ngOnDestroy?
If a component has multiple subscriptions, what is the recommended way to clean them up in ngOnDestroy?
ACall unsubscribe() on each subscription individually in ngOnDestroy
BIgnore unsubscribing because Angular cleans up automatically
CSet each subscription variable to null in ngOnDestroy
DStore all subscriptions in a Subscription object and call unsubscribe() once on it
Attempts:
2 left
💡 Hint
Think about grouping subscriptions for easier cleanup.
state_output
expert
2:00remaining
What is the console output after the component is destroyed?
Given this Angular component, what will be logged to the console after the component is destroyed?
Angular
export class TimerComponent implements OnDestroy {
  count = 0;
  intervalId = setInterval(() => {
    this.count++;
    console.log('Count:', this.count);
  }, 1000);

  ngOnDestroy() {
    clearInterval(this.intervalId);
    console.log('Cleanup done');
  }
}

// Assume component is destroyed after 3500ms
A"Cleanup done", "Count: 1", "Count: 2", "Count: 3"
B"Count: 1", "Count: 2", "Count: 3", "Cleanup done"
C"Count: 1", "Count: 2", "Cleanup done"
D"Count: 1", "Count: 2", "Count: 3"
Attempts:
2 left
💡 Hint
Consider how many intervals run before clearInterval is called at 3500ms.