JSON MCQs – APIs and JSON

11.) What does the Accept header specify in an API request?

A) The client type
B) The server’s API version
C) The authentication type
D) The data format expected in the response

Answer: Option D

Explanation: The Accept header indicates the media type, such as application/json, expected in the response.

12.) How do you handle errors in a fetch() request?

A) Using try…catch blocks or then().catch() methods
B) Using fetchError() function
C) Using the error property of the response
D) Errors cannot be handled in fetch()

Answer: Option A

Explanation: Errors in fetch() requests can be handled with try…catch blocks or chaining a .catch() method.

13.) What is the output of response.ok in a fetch() API call if the request is successful?

A) “ok”
B) true
C) 1
D) null

Answer: Option B

Explanation: The response.ok property returns true if the HTTP status code is in the range of 200–299.

14.) Which function can convert an API response from JSON to JavaScript objects?

A) JSON.stringify()
B) JSON.decode()
C) response.json()
D) response.parse()

Answer: Option C

Explanation: The response.json() function parses JSON responses and returns JavaScript objects.

15.) What is the purpose of the Authorization header in API requests?

A) To specify data format
B) To include authentication credentials
C) To specify the API version
D) To set request priority

Answer: Option B

Explanation: The Authorization header is used to send authentication credentials, such as tokens or API keys, with the request.

16.) Which HTTP method is typically used for sending data to an API?

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

Answer: Option A

Explanation: The POST method is used to send data to an API, such as creating new resources.

17.) What is the purpose of an API key in requests?

A) To encrypt the request data
B) To indicate the request priority
C) To specify the API version
D) To authenticate the client

Answer: Option D

Explanation: API keys authenticate and authorize the client making the request.

18.) Which of the following is a commonly used library for making API requests in JavaScript?

A) jQuery
B) Axios
C) D3.js
D) Lodash

Answer: Option B

Explanation: Axios is a popular library for making API requests in JavaScript. It simplifies making HTTP requests, handling responses, and dealing with errors. It’s widely used in modern web development for its ease of use and additional features compared to the native Fetch API.

19.) What happens when a fetch() request encounters a network error?

A) Throws a TypeError
B) Returns undefined
C) Continues execution without errors
D) Returns an empty object

Answer: Option A

Explanation: A network error in a fetch() request results in a TypeError, which can be handled with error-handling mechanisms.

Leave a Reply

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