Angular MCQs – HTTP and API Integration

21.) How do you set global headers for every HTTP request?

A) Using an HttpInterceptor
B) Setting headers in every HttpClient request
C) Using a config file
D) Using HttpHeaders.setGlobal()

Answer: Option A

Explanation: HTTP interceptors allow setting global headers like authentication tokens for all requests.

22.) What is the main advantage of using Angular HTTPClient?

A) It removes the need for servers
B) It makes API calls faster
C) It supports observables and RxJS operators
D) It works without a backend

Answer: Option C

Explanation: HttpClient integrates seamlessly with RxJS for powerful API request handling.

23.) How do you transform an API response before using it?

A) Using map() in RxJS
B) Using filter()
C) Using delay()
D) Using concatMap()

Answer: Option A

Explanation: The map() operator is used to modify or extract data from an API response before using it.

24.) What is the correct way to handle timeouts in Angular HTTP requests?

A) delay()
B) setTimeout()
C) catchError()
D) timeout() operator in RxJS

Answer: Option D

Explanation: The timeout() operator cancels an HTTP request if it takes too long to respond.

25.) Which operator is used to handle multiple API calls sequentially in Angular?

A) switchMap
B) mergeMap
C) concatMap
D) forkJoin

Answer: Option C

Explanation: concatMap ensures that each API call runs sequentially before moving to the next one.

26.) Which Angular lifecycle hook is best for making API calls?

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

Answer: Option A

Explanation: ngOnInit() is called once when a component is initialized, making it ideal for API calls.

27.) How do you test Angular HTTP services?

A) Using HttpTestClient
B) Using HttpClientTestingModule
C) Using APIUnitTestModule
D) Using HttpMockService

Answer: Option B

Explanation: HttpClientTestingModule allows unit testing HTTP requests without making actual API calls.

28.) What is take(1) used for in Angular HTTP requests?

A) To add a delay before execution
B) To increase API performance
C) To retry the request if it fails
D) To take the first API response and unsubscribe automatically

Answer: Option D

Explanation: take(1) automatically unsubscribes after the first response, preventing memory leaks.

Leave a Reply

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