Question 4Medium
Why avoid heavy logic in the constructor of a component?
- Constructors are forbidden in Angular, so all code must run in ngOnInit
- It should mainly wire dependencies; inputs are not available yet
- Constructors run on every change detection
- 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
- 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?
- constructor, ngOnChanges, ngOnInit, ngDoCheck
- ngOnInit, constructor, ngOnChanges, ngDoCheck
- constructor, ngOnInit, ngOnChanges, ngDoCheck
- 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
- Construct first.
- ngOnChanges only if inputs exist.
- 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?
- ngAfterContentInit
- ngOnInit
- ngDoCheck
- ngAfterViewInit
Answer: D. ngAfterViewInit
Explanation
ngAfterContentInit is for projected content; the view hook is for the component's own template.
Step-by-step solution
- 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.