Bird
Raised Fist0
Angularframework~20 mins

Component lifecycle overview in Angular - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Angular Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when an Angular component is initialized?
Consider an Angular standalone component using signals and the new lifecycle hooks. What is the correct sequence of lifecycle events when the component first appears on screen?
A1,3,4,2
B1,2,3,4
C1,3,2,4
D2,1,3,4
Attempts:
2 left
💡 Hint
Remember that ngOnChanges runs after constructor if there are input changes.
❓ state_output
intermediate
2:00remaining
What is the value of the signal after component initialization?
Given this Angular component snippet using signals, what is the value of the signal 'count' after the component initializes?
Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `Count: {{ count() }}`
})
export class CounterComponent {
  count = signal(0);

  constructor() {
    this.count.set(5);
  }

  ngOnInit() {
    this.count.update(c => c + 1);
  }
}
A6
B5
C0
D1
Attempts:
2 left
💡 Hint
Check the order of constructor and ngOnInit execution and how signals update.
🔧 Debug
advanced
2:00remaining
Why does this Angular component not update the view after input changes?
This Angular component uses signals but the view does not update when the input changes. What is the cause?
Angular
import { Component, Input, signal } from '@angular/core';

@Component({
  selector: 'app-display',
  standalone: true,
  template: `Value: {{ value() }}`
})
export class DisplayComponent {
  value = signal('');

  @Input() set data(val: string) {
    this.value.set(val);
  }
}
AThe signal 'value' is not declared as readonly, causing update errors.
BThe setter does not trigger change detection because signals are not reactive to inputs.
CThe component is missing ngOnChanges lifecycle hook to update the signal.
DThe signal 'value' is initialized after the setter runs, so it resets to empty string.
Attempts:
2 left
💡 Hint
Consider the order of property initialization and setter execution.
📝 Syntax
advanced
2:00remaining
Which option correctly uses the new Angular 17 standalone component lifecycle syntax?
Select the code snippet that correctly uses the new Angular 17 lifecycle syntax with signals and standalone components.
A
}
}  
;)1 + c >= c(etadpu.tnuoc.siht    
{ )(tinInOgn  

}  
;)1(tes.tnuoc.siht    
{ )(rotcurtsnoc  

;)0(langis = tnuoc  
{ tnenopmoCelpmaS ssalc tropxe
)}
`>p/<}} )(tnuoc {{>p<` :etalpmet  
,eurt :enoladnats  
,'elpmas-ppa' :rotceles  
{(tnenopmoC@

;'eroc/ralugna@' morf } tcejni ,langis ,tnenopmoC { tropmi
B
import { Component, signal, inject } from '@angular/core';

@Component({
  selector: 'app-sample',
  standalone: true,
  template: `<p>{{ count() }}</p>`
})
export class SampleComponent {
  count = signal(0);

  ngOnInit() {
    this.count.set(1);
  }

  constructor() {
    this.count.update(c => c + 1);
  }
}
C
import { Component, signal, inject } from '@angular/core';

@Component({
  selector: 'app-sample',
  standalone: true,
  template: `<p>{{ count() }}</p>`
})
export class SampleComponent {
  count = signal(0);

  constructor() {
    this.count.set(1);
  }

  ngOnInit() {
    this.count.update(c => c + 1);
  }
}
D
mport { Component, signal, inject } from '@angular/core';

@Component({
  selector: 'app-sample',
  standalone: true,
  template: `<p>{{ count() }}</p>`
})
export class SampleComponent {
  count = signal(0);

  constructor() {
    this.count.set(1);
  }

  ngOnInit() {
    this.count.update(c => c + 1);
  }
}
Attempts:
2 left
💡 Hint
Check for missing semicolons and correct order of constructor and ngOnInit.
🧠 Conceptual
expert
2:00remaining
Which lifecycle hook is best for accessing child component references in Angular 17?
In Angular 17 standalone components, which lifecycle hook should you use to safely access child component references after the view is fully initialized?
AngAfterViewInit
Bconstructor
CngOnInit
DngAfterContentInit
Attempts:
2 left
💡 Hint
Think about when the component's template and child views are ready.

Practice

(1/5)
1. Which Angular lifecycle hook is called once after the component's data-bound properties have been initialized?
easy
A. ngOnInit()
B. ngOnDestroy()
C. ngAfterViewInit()
D. ngDoCheck()

Solution

  1. Step 1: Understand lifecycle hook purpose

    ngOnInit() runs once after Angular sets component input properties.
  2. Step 2: Compare other hooks

    ngOnDestroy() runs before component removal, ngAfterViewInit() after view initialization, ngDoCheck() on every change detection.
  3. Final Answer:

    ngOnInit() -> Option A
  4. Quick Check:

    Initialization hook = ngOnInit() [OK]
Hint: Initialization after inputs = ngOnInit() [OK]
Common Mistakes:
  • Confusing ngOnInit with ngAfterViewInit
  • Thinking ngOnDestroy runs on init
  • Mixing ngDoCheck with ngOnInit
2. Which of the following is the correct syntax to implement the OnDestroy lifecycle hook in an Angular component?
easy
A. class MyComponent implements OnDestroy { ngDestroy() { } }
B. class MyComponent implements OnDestroy { ngOnDestroy() { } }
C. class MyComponent { onDestroy() { } }
D. class MyComponent implements OnDestroy { destroy() { } }

Solution

  1. Step 1: Recall correct method name

    The lifecycle hook method for destruction is ngOnDestroy().
  2. Step 2: Check interface implementation

    Implementing OnDestroy requires defining ngOnDestroy() exactly.
  3. Final Answer:

    class MyComponent implements OnDestroy { ngOnDestroy() { } } -> Option B
  4. Quick Check:

    OnDestroy method = ngOnDestroy() [OK]
Hint: Method name must be ngOnDestroy() exactly [OK]
Common Mistakes:
  • Using wrong method name like ngDestroy or destroy
  • Not implementing the interface
  • Omitting parentheses in method
3. Given this Angular component code snippet, what will be logged to the console when the component is initialized and then destroyed?
export class SampleComponent implements OnInit, OnDestroy {
  ngOnInit() {
    console.log('Init');
  }
  ngOnDestroy() {
    console.log('Destroy');
  }
}
medium
A. Only Destroy
B. Only Init
C. Init followed by Destroy
D. No output

Solution

  1. Step 1: Understand ngOnInit behavior

    ngOnInit() runs once when component initializes, logging 'Init'.
  2. Step 2: Understand ngOnDestroy behavior

    ngOnDestroy() runs once before component is removed, logging 'Destroy'.
  3. Final Answer:

    Init followed by Destroy -> Option C
  4. Quick Check:

    Init then Destroy logs = Init followed by Destroy [OK]
Hint: Init logs on start, Destroy logs on removal [OK]
Common Mistakes:
  • Assuming ngOnDestroy runs on init
  • Expecting logs in reverse order
  • Thinking no logs appear
4. Identify the error in this Angular component lifecycle hook implementation:
export class TestComponent implements OnInit {
  ngOnInit() {
    console.log('Component initialized');
  }
  ngOnDestroy() {
    console.log('Component destroyed');
  }
}
medium
A. Missing OnDestroy interface in implements clause
B. ngOnInit method name is incorrect
C. ngOnDestroy should not be used here
D. Console.log statements are invalid

Solution

  1. Step 1: Check implemented interfaces

    The class implements OnInit but defines ngOnDestroy() without implementing OnDestroy.
  2. Step 2: Understand interface contract

    To use ngOnDestroy(), the class should implement OnDestroy interface for clarity and correctness.
  3. Final Answer:

    Missing OnDestroy interface in implements clause -> Option A
  4. Quick Check:

    Implement interface for each lifecycle hook used [OK]
Hint: Implement interfaces for all lifecycle hooks used [OK]
Common Mistakes:
  • Ignoring interface implementation for ngOnDestroy
  • Thinking method names are wrong
  • Believing console.log is invalid
5. You want to run code after Angular fully initializes the component's view and its child views. Which lifecycle hook should you use, and why? Choose the correct hook and reason.
hard
A. ngAfterContentInit() because it runs after projected content is initialized.
B. ngOnInit() because it runs after input properties are set.
C. ngDoCheck() because it runs on every change detection cycle.
D. ngAfterViewInit() because it runs after the component's view and child views are initialized.

Solution

  1. Step 1: Identify hook for view initialization

    ngAfterViewInit() runs after Angular initializes the component's view and child views.
  2. Step 2: Compare other hooks

    ngOnInit() runs earlier, ngAfterContentInit() runs after projected content, and ngDoCheck() runs repeatedly.
  3. Final Answer:

    ngAfterViewInit() because it runs after the component's view and child views are initialized. -> Option D
  4. Quick Check:

    View init completion = ngAfterViewInit() [OK]
Hint: Use ngAfterViewInit() for post-view initialization code [OK]
Common Mistakes:
  • Confusing ngAfterContentInit with ngAfterViewInit
  • Using ngOnInit for view-dependent code
  • Expecting ngDoCheck to run once
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard