Angular MCQs – HTTP and API Integration

11.) Which method is used to send data to a server while updating an existing record?

A) http.get()
B) http.put()
C) http.post()
D) http.delete()

Answer: Option B

Explanation: The PUT method is used to update an existing resource on the server.

12.) What is the purpose of http.delete() in Angular?

A) It removes a component
B) It cancels an HTTP request
C) It deletes data from a server
D) It logs errors

Answer: Option C

Explanation: http.delete() is used to send a DELETE request to an API endpoint to remove data.

13.) Which RxJS operator is used to retry an HTTP request if it fails?

A) catchError()
B) retry()
C) delay()
D) map()

Answer: Option B

Explanation: The retry() operator automatically retries a failed request a specified number of times before throwing an error.

14.) What does the finalize() operator do in an HTTP request?

A) Runs a function after the request completes
B) Cancels an API request
C) Logs request details
D) Hides errors from the console

Answer: Option A

Explanation: finalize() ensures that a specified function runs whether the request succeeds or fails.

15.) How can you set global headers for all HTTP requests in Angular?

A) Using globalHeadersService
B) Using HttpHeaders inside each request
C) Using requestOptions
D) Using HttpInterceptor

Answer: Option D

Explanation: Interceptors allow setting headers globally for all API requests, such as authentication tokens.

16.) How do you cancel an ongoing HTTP request in Angular?

A) By calling httpClient.cancel()
B) By using unsubscribe()
C) By using AbortController
D) By setting { cancel: true } in headers

Answer: Option B

Explanation: Calling .unsubscribe() on the HTTP request cancels the request to avoid unnecessary processing.

17.) What is the purpose of tap() in an HTTP request?

A) It modifies the response before returning it
B) It logs or performs side effects without modifying the response
C) It cancels the request
D) It validates the request headers

Answer: Option B

Explanation: tap() is used for logging, debugging, or other side effects without changing the response.

18.) What is observe: ‘response’ used for in an HTTP request?

A) To observe full HTTP response, including headers
B) To only get JSON data
C) To send a GET request
D) To disable request validation

Answer: Option A

Explanation: Setting observe: ‘response’ allows access to the entire response object, not just the body.

19.) How do you handle API pagination in Angular?

A) Using serverPagination module
B) Using pageToken in headers
C) Using queryParams
D) Using paginationService

Answer: Option C

Explanation: Pagination is commonly handled by passing page numbers and limits as query parameters in API requests.

20.) Which method is best for sending large files to a server?

A) http.get()
B) http.post() with FormData
C) http.put() with JSON
D) http.delete()

Answer: Option B

Explanation: FormData is used to send large files efficiently as multipart requests.

Leave a Reply

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