Angular MCQs – Components and Communication

In Angular, components are the building blocks of an application. They define the UI structure, handle user interactions, and manage data flow. Communication between components is crucial for sharing data and achieving modularity.

This MCQ set will help you understand component structure, lifecycle hooks, and data-sharing techniques, which are essential for interview preparation and real-world Angular development.

1.) What is an Angular component?

A) A JavaScript function used to manipulate the DOM
B) A UI building block with an HTML template, TypeScript class, and CSS styles
C) A module that controls application routing
D) A predefined service in Angular

Answer: Option B

Explanation: An Angular component consists of three main parts: template (HTML), logic (TypeScript), and styling (CSS). It is a reusable unit in an Angular application.

2.) How do you define a component in Angular?

A) Using the @Component decorator
B) Using the @Directive decorator
C) Using the @Injectable decorator
D) Using the @Module decorator

Answer: Option A

Explanation: The @Component decorator is used to define Angular components. It provides metadata like selector, template, and styles.

3.) What is the purpose of the selector property in a component?

A) To define the name of the component class
B) To bind the component to a module
C) To set component styles
D) To specify how the component is referenced in templates

Answer: Option D

Explanation: The selector property in @Component defines the custom HTML tag used to insert the component into a template.

4.) Which lifecycle hook is used for cleanup operations?

A) ngOnInit()
B) ngOnChanges()
C) ngOnDestroy()
D) ngAfterViewInit()

Answer: Option C

Explanation: ngOnDestroy() is used for cleaning up resources such as subscriptions before the component is destroyed.

5.) How many components can an Angular application have?

A) Only one
B) Only three
C) Only ten
D) Unlimited

Answer: Option D

Explanation: An Angular application can have any number of components. Each component serves a different part of the UI.

6.) Which component acts as the root component in an Angular project?

A) app.module.ts
B) main.ts
C) app.component.ts
D) index.html

Answer: Option C

Explanation: The root component in an Angular app is AppComponent, which is bootstrapped inside app.module.ts.

7.) What is the default way to pass data from a parent to a child component in Angular?

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

Answer: Option A

Explanation: The @Input decorator allows a parent component to pass data to a child component.

8.) How does a child component communicate with a parent component?

A) Using @Input decorator
B) Using @Output and EventEmitter
C) Using @Injectable service
D) Using @ChildComponent decorator

Answer: Option B

Explanation: The @Output decorator and EventEmitter are used to send events from a child to a parent component.

9.) Which Angular lifecycle hook is called only once when the component is created?

A) ngOnInit()
B) ngOnDestroy()
C) ngAfterViewInit()
D) ngOnChanges()

Answer: Option A

Explanation: The ngOnInit() lifecycle hook runs once after the component initializes.

10.) What is the use of ngOnDestroy() lifecycle hook?

A) It initializes the component
B) It triggers change detection
C) It runs when the view is updated
D) It performs cleanup tasks before a component is destroyed

Answer: Option D

Explanation: ngOnDestroy() is useful for unsubscribing from observables and clearing resources.

Leave a Reply

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