0
0
Angularframework~20 mins

Lifecycle execution order mental model in Angular - Practice Problems & Coding Challenges

Choose your learning style9 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
Angular lifecycle: Initial rendering order
Given an Angular standalone component using signals, which lifecycle hook runs first during initial rendering?
Angular
import { Component, signal, effect } from '@angular/core';

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

  constructor() {
    console.log('constructor');
  }

  ngOnInit() {
    console.log('ngOnInit');
  }

  ngAfterViewInit() {
    console.log('ngAfterViewInit');
  }
}
Aconstructor
BngOnInit
CngAfterViewInit
DngDoCheck
Attempts:
2 left
💡 Hint
Think about what runs first when Angular creates a component instance.
state_output
intermediate
2:00remaining
Signal effect execution timing in Angular
What will be the console output order when this Angular component with a signal and effect runs?
Angular
import { Component, signal, effect } from '@angular/core';

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `<button (click)="increment()">Increment</button>`
})
export class CounterComponent {
  count = signal(0);

  constructor() {
    effect(() => {
      console.log('Effect:', this.count());
    });
  }

  increment() {
    this.count.update(c => c + 1);
  }
}
AEffect: 1 (on init), then Effect: 0 (after click)
BEffect: 0 (on init), then Effect: 1 (after click)
CNo output on init, then Effect: 1 (after click)
DEffect: 0 only once, no output after click
Attempts:
2 left
💡 Hint
Effects run immediately once and then on signal changes.
lifecycle
advanced
2:00remaining
Order of Angular lifecycle hooks with signals and @if control flow
In Angular 17+ with standalone components and signals, what is the correct order of lifecycle hooks when a component with an @if block is first rendered?
Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-toggle',
  standalone: true,
  template: `
    <button (click)="toggle()">Toggle</button>
    @if (visible()) {
      <section>Visible Content</section>
    }
  `
})
export class ToggleComponent {
  visible = signal(false);

  constructor() {
    console.log('constructor');
  }

  ngOnInit() {
    console.log('ngOnInit');
  }

  ngAfterViewInit() {
    console.log('ngAfterViewInit');
  }

  toggle() {
    this.visible.update(v => !v);
  }
}
AngAfterViewInit, constructor, ngOnInit
BngOnInit, constructor, ngAfterViewInit
Cconstructor, ngOnInit, ngAfterViewInit
Dconstructor, ngAfterViewInit, ngOnInit
Attempts:
2 left
💡 Hint
Remember the constructor runs before lifecycle hooks.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in Angular signal usage
Which option contains a syntax error in defining and using a signal in Angular 17+?
A
const count = signal(0);
count.update(c =&gt; c + 1);
B
const count = signal(0)
count.update(c =&gt; c + 1);
C
;)1 + c &gt;= c(etadpu.tnuoc
;)0(langis = tnuoc tsnoc
D
const count = signal(0);
count.set(count() + 1);
Attempts:
2 left
💡 Hint
Check how to update a signal's value correctly.
🔧 Debug
expert
3:00remaining
Why does this Angular component's ngAfterViewInit not run?
An Angular standalone component uses signals and the new @if control flow. The ngAfterViewInit hook never logs. Why?
Angular
import { Component, signal, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-hidden',
  standalone: true,
  template: `
    @if (visible()) {
      <section>Content</section>
    }
  `
})
export class HiddenComponent implements AfterViewInit {
  visible = signal(false);

  constructor() {
    console.log('constructor');
  }

  ngAfterViewInit() {
    console.log('ngAfterViewInit');
  }
}
AThe component must implement AfterViewInit interface to run ngAfterViewInit
BngAfterViewInit is deprecated in Angular 17+ and replaced by signals
CngAfterViewInit only runs if the view has content; here visible() is false so it never runs
DThe constructor blocks ngAfterViewInit from running
Attempts:
2 left
💡 Hint
Check if Angular recognizes the lifecycle hook method.