This set of jQuery MCQs covers key concepts like selectors, events, animations, and AJAX to help you master jQuery in web development.
1. What is jQuery?
a) A programming language
b) A text editor
c) A browser plugin
d) A JavaScript library
View Answer
Explanation: jQuery is a fast, small, and feature-rich JavaScript library designed to simplify DOM manipulation, event handling, and animations.
2. Which of the following is not the feature of jQuery?
a) Efficient query method for finding the set of document elements
b) Expressive syntax for referring to elements in the document
c) Useful set of methods for manipulating selected elements
d) Powerful functional programming techniques is not used for operating on sets of elements as a group
View Answer
Explanation: These features are at the heart of jQuery’s power and utility:
- An expressive syntax (CSS selectors) for referring to elements in the document
- An efficient query method for finding the set of document elements that match a CSS selector
- A useful set of methods for manipulating selected elements
- Powerful functional programming techniques for operating on sets of elements as a group, rather than one at a time
- A succinct idiom (method chaining) for expressing sequences of operations.
3. What is the primary advantage of using jQuery?
a) Faster server-side rendering
b) Compatibility with older browsers
c) Simplifying DOM manipulation and AJAX interactions
d) No need for HTML or CSS knowledge
View Answer
Explanation: jQuery simplifies DOM manipulation, event handling, animations, and AJAX interactions, making web development faster and more accessible.
4. Which of the following is a single global function defined in the jQuery library?
a) $()
b) jQuery()
c) Queryanalysis()
d) global()
View Answer
Explanation: The jQuery library defines a single global function named jQuery(). This function is so frequently used that the library also defines the global symbol $ as a shortcut for it. The $ sign it’s just an alias to jQuery(), then an alias to a function which is used as a selector element.
5. Which of the following is the correct way to include jQuery in an HTML document?
a) <script src=”jquery.js”></script>
b) <script href=”jquery.js”></script>
c) <link src=”jquery.js”>
d) <script name=”jquery.js”></script>
View Answer
Explanation: To include jQuery in an HTML document, the correct method is to use the <script> tag with the src attribute pointing to the jQuery script file (local or hosted). This method allows the browser to load and execute the jQuery code.
6. Which of the following is a factory function?
a) Queryanalysis()
b) jQuery()
c) $()
d) onclick()
View Answer
Explanation: jQuery() is a factory function rather than a constructor: it returns a newly created object but is not used with the new keyword. jQuery objects define many methods for operating on the sets of elements they represent.
7. Which is the JavaScript code that asks for the set of all div elements in a document?
a)
var divs = $(div);
b)
var divs = jQuery("div");
c)
var divs = $("div");
d)
var divs = #("div");
Explanation: :$ sign is used as an selector element and the argument passed to $ function is selected. The code to ask for the set of all div elements in a document is
var divs = $("div");
8. What is the purpose of the jQuery $(document).ready() function?
a) To check if the page is completely loaded
b) To initialize CSS styles
c) To initialize jQuery
d) To check if the page is empty
View Answer
Explanation: $(document).ready() is a jQuery function that runs once the DOM (Document Object Model) is fully loaded, which means that all HTML elements are available for manipulation. This ensures that your jQuery code doesn’t run before the page is ready, preventing errors.
9. Which of the following is a heavily overloaded function?
a) $()
b) script()
c) jQuery()
d) Both $() and jQuery()
View Answer
Explanation: $() is just an alias function of jquery(). The jQuery() function (a.k.a) $()) is the most important one in the jQuery library. It is heavily overloaded, however, and there are four different ways you can invoke it.
10. Which of the following is a utility function in jQuery?
a) jQuery.each()
b) jQuery.noConflict()
c) jQuery.conflict()
d) jQuery.parseJSON()
View Answer
Explanation: jQuery.noConflict() is the utility function in jQuery. The noConflict() method releases the hold on the $ shortcut identifier, so that other scripts can use it.
11. Which method is used to retrieve the value of an input field in jQuery?
a) .getValue()
b) .get()
c) .val()
d) .value()
View Answer
Explanation: The .val() method in jQuery is used to retrieve the value of form input fields (like text boxes, radio buttons, checkboxes, etc.). It can also be used to set the value.
12. How can you remove an element from the DOM using jQuery?
a) .unlink()
b) .detach()
c) .remove()
d) .delete()
View Answer
Explanation: The .remove() method in jQuery is used to completely remove the selected element from the DOM. The .detach() method works similarly but keeps the data and events attached to the element, which can be useful for re-insertion later.
13. How do you bind multiple events to a single element in jQuery?
a) .bind()
b) .combine()
c) .on()
d) .attach()
View Answer
Explanation: The .on() method in jQuery is highly versatile and can bind multiple events to an HTML element, such as click, hover, focus, and more.
14. Which jQuery method is used to add content at the end of an HTML element?
a) .add()
b) .append()
c) .push()
d) .merge()
View Answer
Explanation: The .append() method is used to add content at the end of an HTML element.
15. Which is the method that operates on the return value of $()?
a) show()
b) done()
c) click()
d) css()
View Answer
Explanation: The css() method sets or returns one or more style properties for the selected elements. The css() method operates on the jQuery object returned by $(), and returns that same object, so that the show() method can be invoked next in a compact “method chain.”
16. What does the min mean in the following JavaScript code?
<script src="jquery-1.4.2.min.js"></script>
a) Minimised version
b) Miniature
c) Minimised parameters
d) Minimum value
View Answer
Explanation: The min means the minimised version of the library, with unnecessary comments and whitespace removed, and internal identifiers replaced with shorter ones. The file size of minfile is smaller than the original file hence it makes it easier to load.
17. How do you specify a CSS class to an element in jQuery?
a)
$('#element').addClass('className')
b)
$('#element').setClass('className')
c)
$('#element').setClass()
d)
$('#element').addStyle('className')
Explanation: The .addClass() method in jQuery is used to add one or more CSS classes to an element. This is useful for dynamically changing the appearance of elements on the page.
18. Which of the following is an equivalent replacement of $(document).ready(f)?
a) jQuery(f)
b) #(f)
c) $(f)
d) read(f)
View Answer
Explanation: The equivalent replacement of $(document).ready(f) is $(f). Writing $(document) performs the function of selecting the whole document which is the same as writing $() only.
19. Which method is commonly used to send an AJAX GET request in jQuery?
a) .post()
b) .send()
c) .ajax()
d) .get()
View Answer
Explanation: The .get() method sends an AJAX GET request to the server and returns data.
20. Which method allows you to dynamically change an element’s CSS properties in jQuery?
a) .style()
b) .color()
c) .css()
d) .change()
View Answer
Explanation: The .css() method in jQuery allows you to get or set CSS properties of HTML elements.
21. Which of the following is used for parsing JSON text?
a) jQuery.parseJSON()
b) jQuery.each()
c) jQuery.noConflict()
d) jQuery.conflict()
View Answer
Explanation: jQuery.parseJSON() is used for parsing JSON text. The function converts json to javascript object.
22. What does the $(this).css() method do?
a) Changes the entire CSS of the page
b) Changes the background color of the page
c) Retrieves the current CSS of the page
d) Changes the CSS of the selected element
View Answer
Explanation: $(this).css() is used to get or set CSS properties of the element referenced by this. It is commonly used in event handlers to modify the appearance of the specific element being interacted with.
23. Which method in jQuery is used to find the closest parent element matching a selector?
a) .closest()
b) .ancestor()
c) .locate()
d) .near()
View Answer
Explanation: The .closest() method in jQuery traverses up the DOM tree and finds the nearest ancestor element that matches a specified selector.
24. Why would you use jQuery instead of plain JavaScript?
a) Better browser compatibility
b) Simplified syntax for DOM manipulation and AJAX calls
c) Built-in animation and effects
d) All of the mentioned
View Answer
Explanation: jQuery offers browser compatibility, a simplified syntax for DOM manipulation, easy AJAX integration, and built-in animation and effects, which make web development faster and more efficient.
25. Which method attaches a click event to an HTML button element?
a) .addEvent()
b) .bindClick()
c) .click()
d) .attachEvent()
View Answer
Explanation: In jQuery, the .click() method is the most commonly used way to attach a click event handler. It listens for a click and executes a specified function.
26. How do you handle errors in an AJAX call in jQuery?
a) .onError()
b) .catch()
c) .errorHandler()
d) .fail()
View Answer
Explanation: The .fail() method in jQuery is used to handle errors in AJAX calls, allowing you to debug issues, display error messages, and ensure a better user experience.
27. Which jQuery selector would you use to select all elements with the class highlight?
a) $(“.highlight”)
b) $(“*highlight”)
c) $(“#highlight”)
d) $(“highlight”)
View Answer
Explanation: The $(“.highlight”) selector in jQuery selects all HTML elements that have the class highlight.
28. How do you remove all child elements from a selected element in jQuery?
a) .deleteAll()
b) .clear()
c) .removeAll()
d) .empty()
View Answer
Explanation: The .empty() method removes all child elements and content from the selected element. It leaves the element itself in place, but with no children or inner content.
29. What does the jQuery $.ajax() function do?
a) Makes an asynchronous HTTP request
b) Makes a synchronous HTTP request
c) Makes a request to the database
d) Makes a request to the server and returns the result synchronously
View Answer
Explanation: The $.ajax() function in jQuery is used to perform asynchronous HTTP requests (AJAX). It allows you to fetch data from a server without refreshing the page. The response is handled using callback functions.
30. What does the .wrap() method in jQuery do?
a) Wraps the selected element with a new div
b) Wraps the selected element inside a modal window
c) Wraps the selected element in another element
d) Adds content to the selected element
View Answer
Explanation: The .wrap() method in jQuery wraps the selected elements inside a new HTML element. This is useful for applying a structure or style to a group of elements.
31. What will the following jQuery code do?
$('#myButton').click(function() { alert('Button clicked!'); });
a) Displays an alert when the page loads
b) Displays an alert when the button with id=”myButton” is clicked
c) Hides the button when clicked
d) Displays the button with the text “Button clicked!”
View Answer
Explanation: The code binds a click event handler to the button with the id=”myButton”. When the button is clicked, an alert is shown with the message “Button clicked!”.
32. What will be the output of this jQuery code?
$('#myDiv').css('background-color', 'red');
a) Changes the background color of the page to red
b) Changes the text color of the div with id=”myDiv” to red
c) Hides the div with id=”myDiv”
d) Changes the background color of the div with id=”myDiv” to red
View Answer
Explanation: The .css() method is used to set the background-color property of the div with id=”myDiv” to red.
33. What is the purpose of the following jQuery code?
$('#myDiv').slideDown();
a) Hides the #myDiv element
b) Toggles the visibility of the #myDiv element
c) Shows the #myDiv element by sliding it down
d) Changes the background color of #myDiv
View Answer
Explanation: The .slideDown() method is used to show the #myDiv element with a sliding motion, making it visible from a hidden state.
34. What does this jQuery code do?
$('#myInput').val('New Value');
a) Changes the placeholder of the input field with id=”myInput”
b) Sets the value of the input field with id=”myInput” to “New Value”
c) Sets the background color of the input field with id=”myInput”
d) Clears the value of the input field with id=”myInput”
View Answer
Explanation: The .val() method is used to set the value of form elements like input fields. In this case, it sets the value of the #myInput field to “New Value”.
35. What will be the outcome of the following jQuery code?
$('.item').click(function() { $(this).hide(); });
a) Hides the first .item element
b) Hides the clicked .item element
c) Shows the .item element
d) Displays an alert when any .item element is clicked
View Answer
Explanation: The .click() method attaches an event handler to all elements with the class .item. When any .item element is clicked, it triggers the .hide() method to hide the clicked element.
36. Which symbol is used to select a class in jQuery?
a) #
b) .
c) *
d) %
View Answer
Explanation: In jQuery, the period (.) symbol is used to select elements by class. For example, $(“.class-name”) will select all elements with the class “class-name”.
37. Which jQuery method creates a fade-in effect?
a) .animate()
b) .scroll()
c) .fadeIn()
d) .move()
View Answer
Explanation: The .fadeIn() method creates a smooth fade-in effect by gradually changing the opacity of an element from 0 to 1, making it appear on the page.
38. How do you perform an AJAX POST request in jQuery?
$.ajax({ type: "POST", url: "data.php", data: { name: "John" } });
a) .ajax()
b) .submit()
c) .get()
d) .update()
View Answer
Explanation: The .ajax() method is versatile and can be used to handle multiple types of AJAX requests, including the POST method shown in the example.
39. Which of the following is used to load an external HTML file into a specified HTML element using jQuery?
a) .append()
b) .get()
c) .ajax()
d) .load()
View Answer
Explanation: The .load() method in jQuery allows you to load an external HTML file into a specified element on the page, making it easier to dynamically update content.
40. What does the jQuery selector $(“div p”) do?
a) Selects all <div> elements
b) Selects all <p> elements inside a <div>
c) Selects all <div> and <p> elements
d) Selects all <div> elements with a p class
View Answer
Explanation: The selector $(“div p”) selects all <p> elements that are descendants of <div> elements, regardless of their level in the hierarchy.
41. What does the jQuery method .each() do?
a) Adds a new class to elements
b) Changes the CSS of elements
c) Loops through each selected element
d) Selects the first element
View Answer
Explanation: The .each() method in jQuery allows you to iterate over each selected element and apply a function to each one, making it useful for performing actions on multiple elements.
42. Which of the following events does jQuery not support?
a) .dblclick()
b) .keydown()
c) .scroll()
d) .click()
View Answer
Explanation: While jQuery supports a wide range of events, .dblclick() is not one of them. However, jQuery provides the .click() event to handle click events.
43. How do you trigger an event manually in jQuery?
a) .triggerEvent()
b) .trigger()
c) .manual()
d) .fire()
View Answer
Explanation: The .trigger() method in jQuery is used to manually trigger an event on an element, allowing you to simulate an event without requiring user interaction.
Sanfoundry Global Education & Learning Series – Javascript Programming.
- Practice Programming MCQs
- Practice MCA MCQs
- Check Programming Books
- Check JavaScript Books
- Practice Information Science MCQs