Bird
0
0

Consider this Angular component code snippet:

medium📝 component behavior Q13 of 15
Angular - Lifecycle Hooks
Consider this Angular component code snippet:
export class MyComponent implements OnInit {
  data = '';
  ngOnInit() {
    this.data = 'Hello World';
  }
}

What will be the value of data after the component initializes?
A'' (empty string)
Bundefined
Cnull
D'Hello World'
Step-by-Step Solution
Solution:
  1. Step 1: Check initial value of data

    The property data starts as an empty string.
  2. Step 2: See what ngOnInit does

    Inside ngOnInit, data is set to 'Hello World'. This runs after component creation.
  3. Final Answer:

    'Hello World' -> Option D
  4. Quick Check:

    ngOnInit sets data to 'Hello World' [OK]
Quick Trick: ngOnInit runs after creation, sets data [OK]
Common Mistakes:
  • Assuming data stays empty string
  • Confusing initialization timing
  • Thinking ngOnInit runs before constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes