Docsity

Angular Interview PracticeUltimate Exam, Exams of Technology

Toggle sidebar
bg1
Angular Interview PracticeUltimate
Exam
**Question 1. Which decorator is used to define a class as an Angular service
that can be injected?**
A) @Component
B) @Injectable
C) @Directive
D) @Pipe
Answer: B
Explanation: @Injectable marks a class as available for dependency injection,
allowing Angular to provide it where needed.
**Question 2. What is the purpose of the `bootstrapModule` method in
`main.ts`?**
A) To compile templates at runtime
B) To start the Angular application by loading the root module
C) To register global services
D) To define routing configuration
Answer: B
Explanation: `platformBrowserDynamic().bootstrapModule(AppModule)`
loads the root module and kicks off the Angular app.
**Question 3. Which of the following statements about Angular’s standalone
components is true?**
A) They must be declared in an NgModule’s declarations array.
B) They cannot import other modules.
C) They are declared with `standalone: true` in the @Component metadata.
D) They replace the need for services.
Answer: C
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

Partial preview of the text

Download Angular Interview PracticeUltimate Exam and more Exams Technology in PDF only on Docsity!

Exam

Question 1. Which decorator is used to define a class as an Angular service that can be injected? A) @Component B) @Injectable C) @Directive D) @Pipe Answer: B Explanation: @Injectable marks a class as available for dependency injection, allowing Angular to provide it where needed. Question 2. What is the purpose of the bootstrapModule method in main.ts? A) To compile templates at runtime B) To start the Angular application by loading the root module C) To register global services D) To define routing configuration Answer: B Explanation: platformBrowserDynamic().bootstrapModule(AppModule) loads the root module and kicks off the Angular app. Question 3. Which of the following statements about Angular’s standalone components is true? A) They must be declared in an NgModule’s declarations array. B) They cannot import other modules. C) They are declared with standalone: true in the @Component metadata. D) They replace the need for services. Answer: C

Exam

Explanation: Setting standalone: true makes a component self-contained, allowing it to be used without being declared in an NgModule. Question 4. In Angular, which lifecycle hook is called first when a component is created? A) ngOnInit B) ngAfterViewInit C) ngOnChanges D) ngDoCheck Answer: C Explanation: ngOnChanges runs before ngOnInit and is invoked whenever an @Input property changes, including the first initialization. Question 5. Which syntax correctly binds a component property title to an HTML element’s text content? A) [title]="title" B) {{title}} C) (title)="title" D) bind-title="title" Answer: B Explanation: Interpolation ({{ }}) inserts the value of title into the DOM as text. Question 6. How do you pass data from a child component to its parent? A) Using @Input() B) Using @ViewChild() C) Using @Output() with EventEmitter D) Using a service singleton

Exam

D) async Answer: C Explanation: The CurrencyPipe formats numeric values according to locale-specific currency rules. Question 10. What is the main difference between a pure pipe and an impure pipe? A) Pure pipes can be asynchronous. B) Impure pipes run on every change detection cycle. C) Pure pipes cannot be chained. D) Impure pipes are deprecated. Answer: B Explanation: Pure pipes execute only when their input values change, whereas impure pipes run on every change detection, potentially harming performance. Question 11. Which provider configuration makes a service available only to a specific component and its view children? A) providedIn: 'root' B) providers: [] in the component decorator C) viewProviders: [] in the component decorator D) providedIn: 'any' Answer: C Explanation: viewProviders limits the service to the component’s view hierarchy, excluding content children. Question 12. How would you inject a non-class dependency such as a configuration object?

Exam

A) Using @Injectable() B) Using InjectionToken and the @Inject decorator C) Using @Optional() D) Using @Self() Answer: B Explanation: An InjectionToken provides a type-safe token for non-class values, and @Inject retrieves them. Question 13. What does the @Optional() decorator do when placed on a constructor parameter? A) Throws an error if the dependency is missing. B) Provides a default instance of the dependency. C) Allows the parameter to be null if the provider isn’t found. D) Forces Angular to create a new instance. Answer: C Explanation: @Optional() tells Angular to inject null when the requested provider is not available. Question 14. Which RxJS operator is best suited for canceling previous HTTP requests when a new one is triggered? A) concatMap B) switchMap C) mergeMap D) exhaustMap Answer: B Explanation: switchMap unsubscribes from the previous inner Observable whenever a new outer value arrives, ideal for canceling stale HTTP calls.

Exam

Explanation: The wildcard path '**' matches any URL that isn’t matched by earlier routes, typically used for a “Not Found” page. Question 18. How can you retrieve a route parameter named id inside a component? A) this.router.navigate(['id']) B) this.route.params.subscribe(p => p.id) C) this.route.snapshot.paramMap.get('id') D) Both B and C are valid Answer: D Explanation: You can either subscribe to params Observable or read the snapshot; both give access to the id parameter. Question 19. Which guard interface should you implement to prevent navigation away from a component with unsaved changes? A) CanActivate B) CanDeactivate C) CanLoad D) Resolve Answer: B Explanation: CanDeactivate runs when leaving a route, allowing you to block navigation based on component state. Question 20. What is lazy loading in Angular routing primarily used for? A) Reducing the number of services instantiated. B) Loading feature modules only when the user navigates to their routes, decreasing initial bundle size. C) Pre-fetching all assets during app startup.

Exam

D) Enabling server-side rendering. Answer: B Explanation: Lazy loading splits the application into chunks that are downloaded on demand, improving initial load performance. Question 21. Which directive enables two-way data binding in template-driven forms? A) [ngModel] B) (ngModelChange) C) [(ngModel)] D) ngForm Answer: C Explanation: The banana-in-a-box syntax [(ngModel)] combines property and event binding for two-way binding. Question 22. In Reactive Forms, which class represents a collection of FormControl objects? A) FormArray B) FormGroup C) FormBuilder D) FormControlName Answer: B Explanation: FormGroup aggregates multiple FormControl instances into a single object. Question 23. How do you attach a custom validator to a FormControl in a reactive form? A) Using the validators attribute in the template

Exam

Question 26. What is the main difference between JIT and AOT compilation in Angular? A) JIT compiles templates in the browser; AOT compiles them during the build process. B) JIT produces smaller bundles than AOT. C) AOT requires a special runtime library. D) JIT supports lazy loading, AOT does not. Answer: A Explanation: JIT compiles component metadata in the browser at runtime, while AOT pre-compiles them during the build, resulting in faster startup. Question 27. Which Angular CLI flag enables tree-shaking and minification for a production build? A) --watch B) --aot C) --prod (or --configuration production) D) --source-map Answer: C Explanation: The --prod (or the production configuration) activates optimizations like tree-shaking, minification, and AOT. Question 28. How can you offload heavy calculations from the main thread in an Angular app? A) Using setTimeout B) Using Web Workers C) Using ChangeDetectionStrategy.OnPush D) Using ngZone.runOutsideAngular Answer: B

Exam

Explanation: Web Workers run scripts in a background thread, preventing UI blocking during intensive tasks. Question 29. Which testing utility creates an isolated Angular testing module for a component? A) createComponent B) TestBed.configureTestingModule C) ComponentFixture.detectChanges D) Jasmine.createSpyObj Answer: B Explanation: TestBed.configureTestingModule sets up a testing module that declares components, providers, and imports needed for the test. Question 30. In a unit test, which method is used to query an element inside the component’s template? A) fixture.debugElement.nativeElement.querySelector B) component.getElementById C) TestBed.get D) ng.getElement Answer: A Explanation: fixture.debugElement.nativeElement gives access to the rendered DOM, allowing standard DOM queries. Question 31. Which library provides a Redux-style store for Angular applications? A) RxJS B) NgRx C) AngularFire

Exam

C) By compiling with AOT only D) By adding standalone: true Answer: A Explanation: Angular Elements provides createCustomElement to wrap an Angular component as a custom element (Web Component). Question 35. Which Angular feature allows you to provide translated strings for different locales? A) ngx-translate B) i18n attribute and Angular’s built-in i18n tooling C) @Injectable({ providedIn: 'i18n' }) D) HttpClient interceptors Answer: B Explanation: Angular’s built-in i18n uses i18n attributes and the Angular compiler to extract and replace messages for each locale. Question 36. Which decorator is used to define a custom structural directive? A) @Component B) @Directive with a selector that starts with * C) @Pipe D) @Injectable Answer: B Explanation: A structural directive is created with @Directive and a selector that includes the * prefix, allowing the directive to manipulate the template.

Exam

Question 37. What does the ngOnDestroy lifecycle hook typically contain? A) Initialization logic B) Change detection code C) Cleanup such as unsubscribing from Observables D) Template rendering logic Answer: C Explanation: ngOnDestroy is called just before the component is removed, making it the appropriate place to release resources. **Question 38. Which syntax correctly projects content into a component using ?** A) B) C) D) `` Answer: A Explanation: The select attribute filters projected nodes; [header] projects elements with the header attribute. **Question 39. In Angular forms, what does the updateOn: 'blur' option do? ** A) Validates the control only when the input loses focus. B) Triggers a HTTP request on blur. C) Disables the control after blur. D) Forces immediate change detection. Answer: A

Exam

C) @Pipe D) @Injectable Answer: C Explanation: @Pipe decorates a class that implements PipeTransform, enabling template data transformation. Question 43. What is the effect of setting preserveWhitespaces: true in a component’s metadata? A) Angular removes all whitespace from the template. B) Angular keeps the original whitespace characters during compilation. C) It forces change detection to run on whitespace changes. D) It disables the Angular compiler. Answer: B Explanation: preserveWhitespaces: true tells the compiler not to trim whitespace, preserving formatting as written. Question 44. Which method of the HttpClient returns an Observable of a typed response? A) get(url) B) request(url) C) fetch(url) D) post(url, body, { responseType: 'text' }) Answer: A Explanation: HttpClient.get(url) returns Observable, allowing type-safe HTTP GET requests. Question 45. In Angular routing, what does the pathMatch: 'full' option enforce?

Exam

A) The router matches the route only if the whole URL matches the path. B) The router matches partially. C) It enables lazy loading for the route. D) It disables query parameters. Answer: A Explanation: pathMatch: 'full' ensures that the entire URL segment must match the route’s path, commonly used for redirects. Question 46. Which lifecycle hook is called after Angular projects content into a component (``) ? A) ngAfterContentInit B) ngAfterViewInit C) ngDoCheck D) ngOnChanges Answer: A Explanation: ngAfterContentInit runs after Angular finishes projecting external content into the component. **Question 47. What does the providedIn: 'any' option for a service mean? ** A) The service is a singleton across the entire app. B) A new instance is created for each lazy-loaded module that injects it. C) The service is only available in the root injector. D) It is deprecated. Answer: B Explanation: providedIn: 'any' creates a separate instance in each lazy-loaded module, while still being a singleton within that module.

Exam

Explanation: ComponentFactoryResolver (deprecated) and ViewContainerRef.createComponent allow programmatic component creation. Question 51. Which attribute selector would you use for a structural directive that repeats a template? A) [ngRepeat] B) *ngFor C) ngForOf D) *ngRepeat Answer: B Explanation: *ngFor is the syntax for the built-in structural directive that repeats a template for each item in a collection. Question 52. What is the purpose of the async pipe when used with a BehaviorSubject? A) It converts the BehaviorSubject into a Promise. B) It subscribes to the subject and updates the view on each emission, handling unsubscription automatically. C) It forces synchronous emission. D) It logs each value to the console. Answer: B Explanation: The async pipe handles subscription management, emitting the latest value from the BehaviorSubject to the template. Question 53. Which of the following statements about Angular’s Renderer2 is true? A) It can only be used inside services.

Exam

B) It provides a platform-agnostic way to manipulate the DOM, useful for server-side rendering. C) It replaces the need for ViewChild. D) It is deprecated in Angular 15. Answer: B Explanation: Renderer2 abstracts DOM operations, allowing code to run in environments where direct DOM access isn’t available. Question 54. In a reactive form, how do you retrieve the current value of a FormControl named email? A) this.form.get('email').value B) this.form.value.email C) this.form.controls.email.value D) All of the above Answer: D Explanation: All three expressions correctly access the email control’s value. Question 55. Which Angular feature allows you to define a route that loads a component without a module? A) loadChildren B) loadComponent (standalone component lazy loading) C) canLoad D) resolve Answer: B Explanation: loadComponent (introduced in Angular 14) enables lazy loading of a standalone component directly.