Angular MCQs – Components and Communication

21.) What is the purpose of ngAfterViewInit() lifecycle hook?

A) It initializes the component
B) It runs after the component’s view has been initialized
C) It executes before the component is destroyed
D) It checks for data changes

Answer: Option B

Explanation: ngAfterViewInit() executes after Angular initializes the component’s view, making it useful for accessing @ViewChild properties.

22.) Which of the following is NOT a valid way to share data between Angular components?

A) Using local storage
B) Using @Output()
C) Using @Input()
D) Using a shared service

Answer: Option A

Explanation: While local storage can store data, it is not a direct method for component communication in Angular.

23.) How does Angular detect changes in a component?

A) It manually scans the entire application
B) It listens to browser events
C) It uses a change detection mechanism that checks for state updates
D) It waits for the user to refresh the page

Answer: Option C

Explanation: Angular has a change detection system that updates the UI when data changes.

24.) What does @ViewChild() do in Angular?

A) Allows a component to access a child component or DOM element
B) Creates a new child component dynamically
C) Passes data from a parent component to a child component
D) Manages event listeners in the DOM

Answer: Option A

Explanation: @ViewChild() allows a parent component to get a reference to a child component or DOM element.

25.) Which decorator is used for dependency injection in Angular?

A) @Output
B) @Component
C) @Directive
D) @Injectable

Answer: Option D

Explanation: @Injectable() is used to define services that can be injected into components or other services.

Leave a Reply

Your email address will not be published. Required fields are marked *