Test your knowledge of Angular with this 10-question multiple-choice quiz covering directives, data binding, lifecycle hooks, routing, and more. Ideal for developers seeking to review essential Angular concepts and practical scenarios.
What is Angular primarily used for in the context of modern development?
Explanation: Angular is a widely used framework for building dynamic and interactive web applications, enabling rich user interfaces and complex functionalities. While it can be adapted for various web-based needs, it is not designed for command-line tools or mobile operating systems. Static websites do not require the level of interactivity Angular provides, distinguishing it from frameworks aimed at simple sites. Thus, 'Building dynamic web applications' is the correct answer.
Which of the following is a fundamental building block in Angular applications?
Explanation: Components are the main building blocks in Angular, responsible for controlling portions of the user interface and related logic. Adapters and widgets are not core Angular constructs, and while variables are used in any programming language, they are not a structural concept within Angular itself. Components encapsulate both logic and presentation, making them essential to Angular architecture.
Which Angular CLI command generates a new component called 'user-profile'?
Explanation: The correct command to generate a new component in Angular is 'ng generate component user-profile', which creates the necessary files and boilerplate. 'ng make component' and 'ng add component' are not valid CLI commands. 'ng new component' is also incorrect, as 'ng new' is used for creating a new Angular application, not a component.
Which module must be imported to use the ngModel directive for two-way data binding in Angular?
Explanation: To use ngModel for two-way data binding, the FormsModule must be imported into your Angular app’s module. ReactiveFormsModule is used for reactive forms but does not support ngModel. BrowserModule is essential for running applications in browsers but unrelated to forms, while HttpClientModule is intended for HTTP communication. Only FormsModule enables ngModel.
How can you globally add HTTP headers to all outgoing HTTP requests in Angular?
Explanation: HttpInterceptor is the correct approach to globally intercept and modify HTTP requests, including adding headers, before they are sent. Setting headers with HttpHeaders directly only affects individual requests. Configuring providers in BootstrapModule or modifying headers in ngOnInit for each component is not an effective or scalable solution. Thus, HttpInterceptor provides the intended global effect.
What is the primary effect of a structural directive like *ngIf in an Angular template?
Explanation: Structural directives such as *ngIf control whether a portion of the DOM appears, effectively adding or removing elements based on conditions. Changing color is typically achieved through attribute directives or styling. Two-way binding is handled with ngModel, and event handling uses methods or event directives, not structural directives.
When does the ngAfterViewInit lifecycle hook execute during a component’s lifecycle?
Explanation: The ngAfterViewInit hook runs after Angular fully initializes a component's views, making it ideal for tasks requiring interaction with view children. 'Before the view is created' describes earlier hooks like ngOnInit. Input property changes invoke ngOnChanges, and component destruction triggers ngOnDestroy. Only the correct option denotes the stage after full view initialization.
What does the async pipe in Angular templates automatically do with observables?
Explanation: The async pipe streamlines subscription management by automatically subscribing to observables and cleaning up with unsubscription when the component is destroyed. Waiting for events does not capture the full purpose, and it doesn't convert regular objects into observables. The async pipe does not directly deal with triggering HTTP requests.
Which decorator allows an Angular component to receive data from its parent component?
Explanation: @Input marks a property to receive data from a parent component, enabling component communication. @Output is for emitting data upward from child to parent. @Injectable is used for services, not component properties. @NgModule handles module configuration, not data flow between components.
How does the *ngFor directive function in Angular templates?
Explanation: *ngFor is designed to iterate over an array or collection, generating template instances for each item. Removing elements based on conditions is handled by *ngIf. Listening for custom events involves event binding, not iteration, and input validation is managed by forms and validators, not by *ngFor.