Search Tutorials


AJAX Quiz - MCQ - Multiple Choice Questions | JavaInUse

AJAX Quiz - MCQ - Multiple Choice Questions

Q. What is AJAX used for?

A. Creating dynamic and interactive web pages by exchanging data with a server without refreshing the entire page
B. Only for form validation in web applications
C. Rendering static HTML content
D. Sending data to a server without expecting a response

Q. Which technology is primarily used for sending AJAX requests and receiving responses?

A. JavaScript
B. HTML
C. CSS
D. XML

Q. Which HTTP method is commonly used for sending data to the server in AJAX requests?

A. GET
B. POST
C. PUT
D. PATCH

Q. What is the purpose of the XMLHttpRequest object in AJAX?

A. To send HTTP requests to the server and receive responses
B. To render XML data received from the server
C. To update the HTML content of a web page
D. To validate user input in a form

Q. What is the correct way to create a new XMLHttpRequest object in JavaScript?

A. var xhr = new XMLHttpRequest();
B. var xhr = new XMLHttp();
C. var xhr = XMLHttpRequest();
D. var xhr = new Http();

Q. What is the purpose of the open() method in the XMLHttpRequest object?

A. To open a connection to a specific URL
B. To send data to the server
C. To receive a response from the server
D. To handle errors in the AJAX request

Q. What is the purpose of the readyState property in the XMLHttpRequest object?

A. To check if the request is ready to be sent
B. To determine the current state of the request
C. To handle errors in the AJAX request
D. To check if the response has been received

Q. What is the purpose of the onreadystatechange event handler in AJAX?

A. To handle errors in the AJAX request
B. To update the web page with new data
C. To specify what happens when the readyState changes
D. To send data to the server

Q. What is JSON, and why is it commonly used in AJAX?

A. JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy to read and write for humans and machines.
B. JSON is a programming language used specifically for AJAX data transfer.
C. JSON is a binary format used for efficient data transfer.
D. JSON is a markup language used for structuring data.

Q. How can you handle errors in AJAX requests?

A. By using the onerror event handler
B. By checking the status property of the XMLHttpRequest object
C. By using the try-catch block in JavaScript
D. All of the above





Q. What is the purpose of the responseText property in the XMLHttpRequest object?

A. To access the response data sent by the server
B. To check the status of the AJAX request
C. To handle errors in the response
D. To update the web page with new data

Q. What is the difference between synchronous and asynchronous AJAX requests?

A. Synchronous requests block further code execution until a response is received, while asynchronous requests allow code to continue executing.
B. Synchronous requests are sent using XML, while asynchronous requests use JSON.
C. Synchronous requests are sent to the server immediately, while asynchronous requests are queued and sent later.
D. Synchronous requests are used for small data transfers, while asynchronous requests are used for large data transfers.

Q. How can you update a specific HTML element with data received from an AJAX request?

A. By using the innerHTML property to replace the content of the element
B. By creating a new element and appending it to the existing element
C. By using the outerHTML property to replace the entire element
D. By using the value property to update the element\'s value

Q. What is the purpose of the withCredentials property in the XMLHttpRequest object?

A. To send cookies with cross-domain requests
B. To handle authentication and authorization
C. To specify the credentials for basic authentication
D. To enable CORS (Cross-Origin Resource Sharing)

Q. How can you make an AJAX request to a different domain than the one from which the original page was requested?

A. By using JSONP (JSON with Padding)
B. By setting the withCredentials property to true
C. By enabling CORS (Cross-Origin Resource Sharing) on the server
D. By using a proxy server to forward the request

Q What is AJAX used for in web development?

A.
To create dynamic and interactive web pages by exchanging data with a server without refreshing the entire page.
B.
To manage server-side sessions and maintain user login state.
C.
To render HTML templates on the client-side, reducing the load on the server.
D.
To make HTTP requests to external APIs and display the retrieved data on a web page.

Q Which of the following code snippets demonstrates a basic AJAX request using the XMLHttpRequest object?

A.
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.txt", true);
xhr.send(null);

B.
var xhr = new XMLHttpRequest();
xhr.open("POST", "server.php", false);
xhr.send("name=John&age=30");

C.
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
xhr.open("GET", "file.xml", false);
xhr.send();

D.
var xhr = new XMLHttpRequest();
xhr.open("PUT", "update.php", true);
xhr.send("id=123&value=new_data");

Q How do you handle a successful AJAX request and display the response data on a web page?

A.
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
        document.getElementById("result").innerHTML = xhr.responseText;
    }
};

B.
xhr.onerror = function() {
    console.error("Error:", xhr.statusText);
};

C.
xhr.onload = function() {
    if (xhr.status >= 200 && xhr.status < 300) {
        alert("Success!");
    }
};

D.
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        console.log(xhr.responseText);
    }
};

Q. Which HTTP methods are commonly used with AJAX?

A.
GET and POST
B.
PUT and DELETE
C.
HEAD and PATCH
D.
All of the above

Q. How can you send data to the server using AJAX?

A.
var data = {name: "John", age: 30};
xhr.open("POST", "server.php", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(data));

B.
var formData = new FormData();
formData.append("name", "John");
formData.append("age", 30);
xhr.open("POST", "server.php", true);
xhr.send(formData);

C.
var data = "name=John&age=30";
xhr.open("POST", "server.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);

D.
All of the above

Q. How can you perform an AJAX request using jQuery?

A.
$.ajax({
    url: "server.php",
    type: "POST",
    data: {name: "John", age: 30},
    success: function(response) {
        console.log("Success:", response);
    },
    error: function(error) {
        console.error("Error:", error);
    }
});

B.
$.post("server.php", {name: "John", age: 30}, function(response) {
    console.log(response);
});

C.
$.get("data.txt", function(data) {
    $("#result").html(data);
});

D.
All of the above

Q. What is the purpose of the XMLHttpRequest object's responseXML property?

A.
To access the response data as a JSON object.
B.
To parse and access the response data as an XML document.
C.
To get the raw response data as a string.
D.
To access the response headers sent by the server.

Q. How can you check if AJAX is supported by the user's browser?

A.
if (window.XMLHttpRequest) {
    // AJAX is supported
} else {
    // AJAX is not supported
}

B.
if (window.ActiveXObject) {
    // AJAX is supported
} else {
    // AJAX is not supported
}

C.
if (window.fetch) {
    // AJAX is supported
} else {
    // AJAX is not supported
}

D.
if (window.XMLHttpRequest || window.ActiveXObject) {
    // AJAX is supported
} else {
    // AJAX is not supported
}

Q. What is the purpose of the readyState property in the XMLHttpRequest object?

A.
To check if the AJAX request is in progress.
B.
To determine if the response data is ready to be processed.
C.
To track the number of bytes received in the response.
D.
To check if the server has responded with a status code.

Q. How can you handle errors that occur during an AJAX request?

A.
xhr.onerror = function() {
    console.error("An error occurred:", xhr.statusText);
};

B.
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status >= 400) {
        console.error("Error:", xhr.statusText);
    }
};

C.
xhr.onload = function() {
    if (xhr.status < 200 || xhr.status >= 300) {
        console.error("Error:", xhr.statusText);
    }
};

D.
xhr.onabort = function() {
    console.error("Request aborted:", xhr.statusText);
};