Lifecycle Hooks Questions with Answers

6 practice questions on Lifecycle Hooks (Angular) with answers and step-by-step solutions. Try 6 samples below, then practise all 6 free in the AptiNest app.

Sample questions

Question 1Easy

Which lifecycle hook runs once after the first data-bound inputs are set?

  1. ngOnInit
  2. constructor, which receives input values already bound
  3. ngOnDestroy, which runs after the view is removed
  4. ngAfterViewChecked, which runs only on the server

Answer: A. ngOnInit

Explanation

ngOnInit is the usual place for initialisation that needs inputs.

Step-by-step solution

  1. Inputs are set after construction.
  2. ngOnInit follows the first ngOnChanges.

Why the other options are wrong

  • B. Inputs are not bound during construction.
  • C. That hook is for cleanup.
  • D. It has no such server-only rule.

Question 2Easy

Which hook is the right place to clean up subscriptions and timers?

  1. ngOnInit
  2. ngDoCheck
  3. ngOnDestroy
  4. ngAfterContentInit

Answer: C. ngOnDestroy

Explanation

It runs just before Angular destroys the component.

Step-by-step solution

  1. Cleanup at destruction.

Why the other options are wrong

  • A. ngOnInit runs at start-up; cleanup belongs in ngOnDestroy.
  • B. ngDoCheck runs on every check; it is not the cleanup hook.
  • D. Runs after content projection.

Question 3Medium

Which hook receives a SimpleChanges object describing changed @Input values?

  1. ngOnInit
  2. ngDoCheck
  3. ngAfterViewInit
  4. ngOnChanges

Answer: D. ngOnChanges

Explanation

It fires before ngOnInit and whenever a bound input reference changes.

Step-by-step solution

  1. Inputs change -> ngOnChanges.

Why the other options are wrong

  • A. ngOnInit runs once and receives no SimpleChanges object.
  • B. ngDoCheck runs every cycle and gets no changes object.
  • C. ngAfterViewInit runs after view init, not when inputs change.

Question 4Medium

Why avoid heavy logic in the constructor of a component?

  1. Constructors are forbidden in Angular, so all code must run in ngOnInit
  2. It should mainly wire dependencies; inputs are not available yet
  3. Constructors run on every change detection
  4. Constructors cannot be async

Answer: B. It should mainly wire dependencies; inputs are not available yet

Explanation

Initialisation using inputs belongs in ngOnInit.

Step-by-step solution

  1. Constructor = DI wiring.

Why the other options are wrong

  • A. Constructors are allowed; Angular only advises light work there.
  • C. Constructors run once per instance, not on each change detection.
  • D. Async is not the issue.

Question 5Medium

What is the order of the first run of these hooks?

  1. constructor, ngOnChanges, ngOnInit, ngDoCheck
  2. ngOnInit, constructor, ngOnChanges, ngDoCheck
  3. constructor, ngOnInit, ngOnChanges, ngDoCheck
  4. ngOnChanges, constructor, ngDoCheck, ngOnInit

Answer: A. constructor, ngOnChanges, ngOnInit, ngDoCheck

Explanation

The class is constructed, inputs set, then ngOnInit, then ngDoCheck.

Step-by-step solution

  1. Construct first.
  2. ngOnChanges only if inputs exist.
  3. ngOnInit then ngDoCheck.

Why the other options are wrong

  • B. Constructor always comes first.
  • C. ngOnChanges precedes ngOnInit.
  • D. The constructor always runs first, before ngOnChanges.

Question 6Hard

Which hook is called after the component's view and child views are initialised?

  1. ngAfterContentInit
  2. ngOnInit
  3. ngDoCheck
  4. ngAfterViewInit

Answer: D. ngAfterViewInit

Explanation

ngAfterContentInit is for projected content; the view hook is for the component's own template.

Step-by-step solution

  1. View vs content.

Why the other options are wrong

  • A. ngAfterContentInit runs after projected content, before the view is ready.
  • B. ngOnInit runs before the view and child views are initialised.
  • C. ngDoCheck runs on every check, not once after view initialisation.

Practise all 6 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