0
0
Angularframework~20 mins

ngOnInit for initialization in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ngOnInit Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when ngOnInit runs in this Angular component?

Consider this Angular component code snippet. What will be the value of message after ngOnInit runs?

Angular
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-welcome',
  template: `<p>{{ message }}</p>`,
  standalone: true
})
export class WelcomeComponent implements OnInit {
  message = '';

  ngOnInit() {
    this.message = 'Hello from ngOnInit!';
  }
}
A"" (empty string)
Bnull
Cundefined
D"Hello from ngOnInit!"
Attempts:
2 left
💡 Hint

ngOnInit runs after the component is created but before it is displayed.

lifecycle
intermediate
1:30remaining
When is ngOnInit called in Angular component lifecycle?

Choose the correct timing for when ngOnInit is called in an Angular component's lifecycle.

AAfter the component's constructor and before the first render
BBefore the component's constructor runs
CAfter the component is destroyed
DOnly when the component's inputs change
Attempts:
2 left
💡 Hint

Think about when initialization logic should run relative to component creation.

📝 Syntax
advanced
2:00remaining
Identify the correct ngOnInit implementation in this Angular component

Which option correctly implements ngOnInit in this Angular component?

Angular
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-counter',
  template: `<p>{{ count }}</p>`,
  standalone: true
})
export class CounterComponent implements OnInit {
  count = 0;

  // ngOnInit implementation goes here
}
AngOnInit() => { this.count = 10; }
BngOnInit() { this.count = 10; }
Cfunction ngOnInit() { this.count = 10; }
DngOnInit { this.count = 10; }
Attempts:
2 left
💡 Hint

Remember the correct method syntax inside a class in TypeScript.

🔧 Debug
advanced
2:30remaining
Why does this ngOnInit code not update the template?

Given this Angular component, why does the template not show the updated title after ngOnInit?

Angular
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-title',
  template: `<h1>{{ title }}</h1>`,
  standalone: true
})
export class TitleComponent implements OnInit {
  title = 'Initial';

  ngOnInit() {
    let title = 'Updated in ngOnInit';
  }
}
AThe component selector is missing, so it does not render
BngOnInit is not called automatically, so the title stays the same
CThe local variable 'title' inside ngOnInit shadows the class property, so the class property is not changed
DThe template binding syntax is incorrect and does not update
Attempts:
2 left
💡 Hint

Check variable scope inside ngOnInit.

🧠 Conceptual
expert
3:00remaining
What is the main benefit of using ngOnInit instead of constructor for initialization?

Why should you put initialization code in ngOnInit rather than the constructor in Angular components?

AngOnInit runs after Angular sets input properties, so initialization can use them safely
BThe constructor runs multiple times, but ngOnInit runs only once
CngOnInit runs before the constructor, so it initializes earlier
DThe constructor cannot access class properties, but ngOnInit can
Attempts:
2 left
💡 Hint

Think about when Angular sets input properties relative to constructor and ngOnInit.