JavaScript MCQs – Ajax

11.) How do you set a custom HTTP header in an AJAX request?

A) addHeader()
B) setRequestHeader()
C) customHeader()
D) appendHeader()

Answer: Option B

Explanation: The setRequestHeader() method is used to set custom HTTP headers for a request.

12.) What is the purpose of the abort() method in AJAX?

A) Cancels an ongoing request
B) Logs errors in the request
C) Resends the request
D) Closes the connection

Answer: Option A

Explanation: The abort() method stops an ongoing XMLHttpRequest operation.

13.) Which HTTP response code indicates a successful request?

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

Answer: Option D

Explanation: A 200 status code indicates that the request was successful.

14.) What does a 404 HTTP response code mean?

A) Server error
B) Resource not found
C) Request timeout
D) Forbidden request

Answer: Option B

Explanation: A 404 code indicates that the requested resource could not be found on the server.

15.) Which AJAX technique updates only parts of a web page?

A) Partial rendering
B) DOM parsing
C) Page refresh
D) Lazy loading

Answer: Option A

Explanation: AJAX allows partial rendering, meaning only specific parts of the web page are updated without refreshing the entire page.

16.) How do you handle errors in the Fetch API?

A) Using a catch() block
B) Using try…catch
C) Using onerror
D) Using then()

Answer: Option A

Explanation: Errors in the Fetch API are handled using the catch() block of a promise chain.

17.) What is the default content type for POST requests in AJAX?

A) text/plain
B) application/json
C) application/x-www-form-urlencoded
D) multipart/form-data

Answer: Option C

Explanation: The default content type for POST requests in AJAX is application/x-www-form-urlencoded.

18.) What is JSONP in the context of AJAX?

A) A modern AJAX API
B) A method to fetch JSON data from other domains
C) A JSON parser for browsers
D) A replacement for XMLHttpRequest

Answer: Option B

Explanation: JSONP is used to bypass same-origin policy restrictions by loading JSON data via script tags.

19.) Which HTTP method is idempotent?

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

Answer: Option C

Explanation: The PUT method is idempotent, meaning multiple identical requests have the same effect.

20.) What does the credentials option in the Fetch API specify?

A) Include cookies in requests
B) Redirect behavior
C) Cache policy
D) Request timeout

Answer: Option A

Explanation: The credentials option controls whether cookies or credentials are included in requests.

Leave a Reply

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