Sample questions
Question 1Easy
Which lifecycle hook should be used to release subscriptions and timers when a component is removed?
- ngOnInit
- ngAfterViewInit
- ngOnDestroy
- ngOnChanges
Answer: C. ngOnDestroy
Explanation
ngOnDestroy runs just before Angular destroys the component, the right place for cleanup.
Step-by-step solution
- NgOnDestroy runs when a component is about to be destroyed.
- It is used to clean up resources like subscriptions and timers.
- Subscriptions and timers should be unsubscribed or stopped here.
- This ensures no memory leaks or unexpected behavior.
- The correct answer is ngOnDestroy.
Why the other options are wrong
- A. ngOnInit runs at start-up for setup, not when the component is removed, so it is not for cleanup.
- B. ngAfterViewInit runs after the view is ready, which is setup timing, not cleanup on removal.
- D. ngOnChanges runs when inputs change, not when the component is removed, so it is not for cleanup.
Tip: Use ngOnDestroy for cleaning up resources in Angular components.
Question 2Easy
Which file typically contains a component's logic?
- The .scss file
- The .ts class file
- The .spec file only
- The index.html file
Answer: B. The .ts class file
Explanation
Template and styles are separate or inline.
Step-by-step solution
- Class holds logic.
Why the other options are wrong
- A. The .scss file holds styles, not the component's logic.
- C. The .spec file holds tests, not the component's logic.
- D. index.html is the app shell, not a component's logic file.
Question 3Easy
In @Component metadata, which property defines the HTML tag used to place the component in a template?
- template
- selector
- providers
- styleUrls
Answer: B. selector
Explanation
The selector is the CSS selector matched in other templates, such as 'app-card'.
Step-by-step solution
- Which property names the tag?
Why the other options are wrong
- A. template holds the markup.
- C. providers lists services.
- D. styleUrls lists style files.
Question 4Easy
Which decorator marks a TypeScript class as an Angular component?
- @Component
- @Injectable
- @Directive only
- @NgModule
Answer: A. @Component
Explanation
@Component attaches metadata such as the selector and template to a class so Angular can render it as a view.
Step-by-step solution
- @Component is used to define an Angular component class.
- It provides metadata like selector and template for rendering.
- Other decorators like @Injectable or @NgModule have different purposes.
- Only @Component marks a TypeScript class as an Angular component.
- Final answer: @Component.
Why the other options are wrong
- B. @Injectable marks a service for dependency injection, not a component class.
- C. @Directive only is wrong; a component uses @Component, and a directive uses a different decorator.
- D. @NgModule groups components and declarations into a module; it does not mark a component.
Tip: @Component is required for any Angular component.
Question 5Medium
Which feature lets a parent pass HTML into a child component's template at a marked place?
- Interpolation of a string into the child's template using braces
- Property binding applied to the child's host element only
- Template reference variables declared in the parent template
- Content projection using <ng-content>
Answer: D. Content projection using <ng-content>
Explanation
<ng-content> in the child template marks where the parent's nested content is rendered.
Step-by-step solution
- The question asks how a parent places its HTML inside a child component.
- The child template marks the spot with the ng-content tag.
- This is content projection. The correct answer is D (Content projection using <ng-content>).
Why the other options are wrong
- A. Braces interpolate text; they do not place nested HTML content.
- B. Property binding sets one property; it does not project markup.
- C. Template reference variables name elements; they do not place content.
Tip: The parent writes content inside the child tag, and the child places it with ng-content.
Question 6Medium
In what order do these hooks run on first creation: ngOnInit, ngOnChanges, constructor?
- ngOnInit, constructor, ngOnChanges
- constructor, ngOnChanges, ngOnInit
- ngOnChanges, constructor, ngOnInit
- constructor, ngOnInit, ngOnChanges
Answer: B. constructor, ngOnChanges, ngOnInit
Explanation
The class is constructed first, inputs are set (ngOnChanges if any are bound), then ngOnInit runs once.
Step-by-step solution
- Constructor runs first to initialize the class.
- NgOnChanges runs next to handle input changes.
- NgOnInit runs last for setup after construction.
Why the other options are wrong
- A. The constructor must run first, before any hook, so ngOnInit cannot come before it.
- C. ngOnChanges runs after the constructor, not before it, so this order is wrong.
- D. ngOnChanges runs before ngOnInit on first creation, so placing ngOnInit ahead of it is wrong.
Tip: Remember: constructor → ngOnChanges → ngOnInit.
Question 7Medium
What does @ViewChild('box') el give you, and when is it first reliably available?
- A reference to the parent component, available right in the constructor
- A cloned copy of the DOM node, available in ngOnChanges every time
- A reference to a template element or component, ready after ngAfterViewInit
- A projected content node, available in ngOnInit under all conditions
Answer: C. A reference to a template element or component, ready after ngAfterViewInit
Explanation
View queries are resolved once the view is created; for static=false queries use ngAfterViewInit.
Step-by-step solution
- ViewChild gets a reference to a template element or component.
- It is available after the view is initialized in ngAfterViewInit.
- Static false queries require ngAfterViewInit for reliable access.
- NgOnInit runs before the view is fully initialized.
- The correct answer is a reference to a template element or component, ready after ngAfterViewInit.
Why the other options are wrong
- A. ViewChild does not return the parent component, and the constructor is too early for view queries.
- B. ViewChild does not return a cloned DOM node in ngOnChanges; it returns a live reference.
- D. A projected content node is ContentChild territory; ViewChild gives view elements, not projected content.
Tip: Use ngAfterViewInit for ViewChild to get reliable access to DOM elements.
Question 8Medium
What does a template reference variable such as #box on an input give you?
- A reference to the component class instance that owns this template view
- A two-way binding to a form model
- A new instance of a service
- A reference to that DOM element usable elsewhere in the same template
Answer: D. A reference to that DOM element usable elsewhere in the same template
Explanation
#box makes the element available as 'box' within the template.
Step-by-step solution
- Hash declares variable.
- Scope is the template.
Why the other options are wrong
- A. It does not refer to the component.
- B. No binding is created.
- C. A template reference variable points at an element, not a service instance.
Question 9Hard
A child has @Input() set value(v) { this._v = v * 2 }. The parent binds [value]="5". What is read from _v?
- 10, because the setter runs on each binding update
- 5, because setters are ignored for inputs
- undefined until ngOnInit
- It throws a template error
Answer: A. 10, because the setter runs on each binding update
Explanation
Input setters run whenever the bound value changes, so _v becomes 10.
Step-by-step solution
- Setter receives 5.
- Multiplies by 2.
Why the other options are wrong
- B. The setter is not ignored; the input is doubled to 10.
- C. The setter runs before ngOnInit.
- D. The setter runs normally, so no template error is thrown.
Question 10Hard
Which statement about @ViewChild is correct?
- It is available in the constructor, since Angular renders the view before running the constructor
- The queried element is available after the view initialises, typically in ngAfterViewInit
- It reads the parent component's template, not its own
- It only works with services
Answer: B. The queried element is available after the view initialises, typically in ngAfterViewInit
Explanation
View queries resolve after the template renders (unless static: true for non-conditional nodes).
Step-by-step solution
- Template must exist first.
- Use ngAfterViewInit.
Why the other options are wrong
- A. Not set yet at construction.
- C. ViewChild queries the component's own view, not the parent's template.
- D. It queries view nodes.
Practise all 25 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 →