JavaScript MCQs – Ajax

21.) How can you handle both the response and errors in the Fetch API?

A) Using catch() only
B) Using then() only
C) Using then() and catch()
D) Using finally()

Answer: Option C

Explanation: Both responses and errors in the Fetch API can be handled using then() for success and catch() for errors.

22.) What is the responseType property used for in XMLHttpRequest?

A) To define the format of the response
B) To define the status code of the response
C) To define the method used in the request
D) To set the request headers

Answer: Option A

Explanation: The responseType property is used to define the expected format of the response, such as text, JSON, or XML.

23.) What is the purpose of the timeout property in XMLHttpRequest?

A) To delay the request
B) To specify the time before the request is automatically aborted
C) To log the request duration
D) To control the server’s response time

Answer: Option B

Explanation: The timeout property specifies the time in milliseconds before an XMLHttpRequest is automatically aborted.

24.) Which method is used to open a request in XMLHttpRequest?

A) openRequest()
B) beginRequest()
C) open()
D) start()

Answer: Option C

Explanation: The open() method is used to initialize a request by setting the request method (e.g., GET, POST) and the URL.

25.) Which of the following methods is used to convert a JavaScript object to JSON format?

A) JSON.parse()
B) JSON.stringify()
C) JSON.convert()
D) JSON.toJSON()

Answer: Option B

Explanation: The JSON.stringify() method is used to convert a JavaScript object into a JSON string.

26.) What type of data is commonly used in an AJAX response?

A) Plain text
B) JSON
C) XML
D) All of the above

Answer: Option D

Explanation: AJAX responses can contain various types of data, such as plain text, JSON, or XML, depending on the request and server setup.

27.) Which of these is an example of an asynchronous task in AJAX?

A) Page reload
B) Form submission
C) Data fetching from a server
D) File download

Answer: Option C

Explanation: Fetching data from a server via AJAX is an asynchronous task, meaning it doesn’t block the execution of other code.

28.) How do you prevent the default form submission in AJAX?

A) By calling event.preventDefault()
B) By using form.submit()
C) By calling event.stopPropagation()
D) By setting method=”get”

Answer: Option A

Explanation: event.preventDefault() is used to stop the form from submitting traditionally, allowing you to handle the submission with AJAX instead.

Leave a Reply

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