Angular MCQs – HTTP and API Integration

Working with HTTP and API integration is a crucial skill for any Angular developer. It allows applications to communicate with back-end services, fetch or send data, and handle responses effectively.

The following MCQs will help you understand the HTTP client module, API calls, error handling, interceptors, and best practices in Angular. By practicing these questions, you will be well-prepared for Angular interviews and real-world projects.

1.) Which module is required to make HTTP requests in Angular?

A) HttpModule
B) CommonModule
C) FormsModule
D) HttpClientModule

Answer: Option D

Explanation: Angular provides the HttpClientModule, which must be imported in app.module.ts to enable HTTP communication.

2.) Which Angular service is used to make HTTP calls?

A) Http
B) HttpClient
C) HttpService
D) ApiService

Answer: Option B

Explanation: HttpClient is the recommended service for making HTTP requests in Angular.

3.) Which Angular decorator is used to inject HttpClient into a service?

A) @Injectable
B) @Component
C) @Module
D) @HttpService

Answer: Option A

Explanation: The @Injectable decorator is required to inject dependencies like HttpClient into services.

4.) What does an HTTP interceptor do in Angular?

A) It cancels API requests
B) It logs user actions
C) It modifies HTTP requests and responses globally
D) It initializes API calls

Answer: Option C

Explanation: Interceptors are used to modify HTTP requests, such as adding authentication tokens or handling errors globally.

5.) Which RxJS operator is commonly used to handle API responses?

A) map
B) filter
C) mergeMap
D) switchMap

Answer: Option A

Explanation: The map operator transforms API responses before passing them to components.

6.) How do you handle errors in an Angular HTTP request?

A) Using errorResponse()
B) Using throwError()
C) Using handleError()
D) Using catchError()

Answer: Option D

Explanation: The catchError() operator helps handle API errors and prevents application crashes.

7.) What is the default HTTP request method used by httpClient if not specified?

A) GET
B) POST
C) PUT
D) DELETE

Answer: Option A

Explanation: If no HTTP method is specified, Angular assumes a GET request by default.

8.) How can you send JSON data in a POST request in Angular?

A) { headers: ‘application/json’ }
B) { body: JSON.stringify(data) }
C) { headers: { ‘Content-Type’: ‘application/json’ } }
D) { contentType: ‘json’ }

Answer: Option C

Explanation: To send JSON data, you must set the Content-Type header to application/json.

9.) What does the .subscribe() method do in an HTTP request?

A) Sends an API request
B) Executes the request and listens for a response
C) Cancels the request
D) Caches the response

Answer: Option B

Explanation: .subscribe() is required to execute an HTTP request and process the response.

10.) Which status code represents a successful API request?

A) 200
B) 400
C) 500
D) 404

Answer: Option A

Explanation: A 200 OK status code indicates a successful API response.

Leave a Reply

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