JQUERY – DOT NET Chapter Wise Interview Questions

JQUERY – DOT NET Chapter Wise Interview Questions

Question 1:
What is jQuery?
Answer:
jQuery is a reusable JavaScript library which simplifies JavaScriptcoding. So rather than writing lengthy JavaScript code as shown below you can use jQuery,

By jQuery the above JavaScript code is now simplified as shown below.

Question 2:
So will jQuery replace JavaScript?
Answer:
No, jQuery is not meant to replace JavaScript. jQuery is a library while JavaScript is a language. jQuery sits on the top of JavaScript to make your development easy.

Question 3:
So how do we use these reusable jQuery libraries?
Answer:
You need to download jQuery.js file from jQuery.com and include the same in your Web pages. The jQuery files are named with version number like “jQuery-1.4.1 .js” where 1.4.1 is the version of the JavaScript file. So at the top of your Web page you need to include the JavaScript as shown in the below code.

Question 4:
What is CDN (Content Delivery Network)?
Answer:
In CDN multiple copies of the Website is copied on different geographical servers. When users send request for Website content which have CDN-enabled depending on their geographical location, content is served from the nearest geographical location server to the user.
jquery-dot-net-chapter-wise-interview-questions-7.1
So if a user is from India, the Indian CDN server will serve request for Indian users. This leads to faster delivery of data.

Question 5:
For jQuery files which are the popular CDN’s?
Answer:
There are two popular CDN’s: Microsoft and Google.
If you want to reference Google CDN JQuery files you can use the below script.

If you want to use Microsoft CDN you can use the below JavaScript.

Question 6:
How can we reference local jQuery files if CDN fails?
Answer:
Many times it is possible that Microsoft and Google servers can go down for some time. So in those situations you would like your page to reference jQuery files from local server.
So to implement a CDN fallback is a two-step process:
First reference is the CDN jQuery. In the below code you can see we have reference Microsoft CDN jQuery file.

Now if Microsoft CDN is down then the jQuery value will be “undefined”. So you can see in the below code we are checking if the jQuery is having “undefined” value then do a document write and reference your local jQuery files.

Below is the full code for the same.

Question 7:
What is the difference between jQuery.js and jQuery.min.js file?
Answer:
First thing both the files provide the same jQuery functionalities. One is a long version and the other is compressed/’minified version. The minified version is compressed to save bandwidth and space by compressing, and removing all the white spaces.
Figure 7.2: jQuery. js
jquery-dot-net-chapter-wise-interview-questions-7.2
Figure 7.3 shows the view of jQuery.min.js file (compressed and minified).
jquery-dot-net-chapter-wise-interview-questions-7.3
Question 8:
When should we use jQuery.js over jQuery.min.js?
Answer:
When you are doing development use “jQuery.js” file because you would like to debug the JavaScript code, etc. Use “jQuery.min.js” for production environment. In production / live environment we would like to consume less bandwidth, we would like to our pages to load faster.

Question 9:
What is the use jQuery.vsdoc.js?
Answer:
This file you can include if you want to enable Intellisense in Visual Studio for jQuery as shown in Figure 7.4.
jquery-dot-net-chapter-wise-interview-questions-7.4
Question 10:
How does the basic syntax of JQuery looks like?
Answer:
jQuery syntax structure can be broken down into four parts:

  • All JQuery commands start with a “$” sign as shown in Figure 7.5.
    jquery-dot-net-chapter-wise-interview-questions-7.5
  • Followed by the selection of the HTML element, For example below is a simple image where we are selecting a HTML textbox by id “txt1”.
  • Then followed by the dot (.) separator. This ODerator will separate the element and the action on the element.
  • Finally what action you want to perform on the HTML element. For instance in the below jQuery code we are setting the text value to “Hello jQuery’.

Question 11:
What is the “$” sign in jQuery?
Answer:
The “$” sign is an alias for jQuery.

Question 12:
When should we use jQuery.noConflict()?
Answer:
There are many JavaScript frameworks like MooTools, Backbone, Sammy, Cappuccino, Knockout, etc. Some of these frameworks also use “$” sign so this can lead to conflict with jQuery framework.
So you can use the “noConflict” method and release the jQuery “$” sign as shown in the below code.

You can also create your own jQuery shortcut as shown below.

Question 13:
What are the different ways by which you can select a HTML element in jQuery?
Answer:
You can select jQuery elements in the following ways:

Select all: Below is a simple code snippet which selects all elements and hides them.

Select by Element: Below code selects all paragraph element tags in the HTML and hides them.

Select by ID: If your HTML elements have Id’s like the below HTML code and you want to select the same by id.

Below is how the jQuery code by ID selection code would look like.

Select by class: If your HTML element has class names and you want to select them in jQuery then you need to use the dot operator followed by the classname as shown in the below code.

Select by class and element: Consider the below HTML code where we have elements with “class” names.

So if you wish to select by “element” and “class” in jQuery your selector code becomes something as below.

Select using Equal method Select using Find method Select using Filter method

Question 14:
What is the use of document.ready in jQuery?
Answer:
“document. ready” event occurs once the complete HTML DOM is loaded. So the next question is when do we actually need this event?. Consider the below simple code where we are trying to set a text box “text1” with value “Sometext”.
Now at the point when JQuery code tries set the textbox value, at that moment that text box is not available in the HTML DOM (Document Object Model). So it throws an exception for the same.

So we would like to execute the JQuery code which sets the textbox value only when all the HTML objects are loaded in DOM. So you can replace the code of setting text box value to something as shown below.

Question 15:
Can we have two document. ready in a Web page?
Answer:
Yes.

Question 16:
How can we attach a method to a HTML element event using JQuery?
Answer:
Below is a simple code which attaches a function to click event of a button.

Below is one more example where we have attached the function to a mouseenter event of a paragraph.

Question 17:
How can we add a style using jQuery?
Answer:

Question 18:
What is JSON?
Answer:
JSON (JavaScript Object Notation) is a simple data exchange format which helps to communicate between JavaScript and server-side technologies like Servlets, JSP (Java Servlet Pages), WCF (Windows Communication Foundation), ASP.NET or Active Server Pages .NET etc.
The format of JSON is as shown below. The below format represents a “Customer” object with “CustomerCode” and “CustomerName” properties as shown in Figure 7.6.

Now this format can be easily consumed by JavaScript and transformed into JavaScript object. Look at the Figure 7.6 where we have provided JSON to JavaScript variable and you can see how a JavaScript object is evaluated with “CustomerCode”  and “CustomerName” properties.
jquery-dot-net-chapter-wise-interview-questions-7.6
If you look at the Web architecture it has two parts: browser and server. On the browser side JavaScript is the most prominent and well established language while on server-side you have different technologies like JSP, ASP.NET, PHP (a recursive backronym; HyperText Preprocessor) etc.
jquery-dot-net-chapter-wise-interview-questions-7.7
So if the server-side technologies emit out JSON format which can be easily transformed into a JavaScript object that would really ease the communication between multiple server-side technologies and JavaScript language.

Question 19:
How to convert string to JSON object?
Answer:
“JSON.parse” helps us to convert string to JSON object.

Question 20:
Was not SOAP meant to do the same thing which JSON does?
Answer:
SOAP is heavy due to XML tags. For example, a SOAP message “<Name>Shiv</Name>” will become short, sweet and light in JSON like “Name”: “Shiv”. Second most important it evaluates as JavaScript object. To convert the complicated SOAP XML (extensible Markup Language) as shown in Figure 7.8 in to javascript JSON object would be a tough and tedious task.
jquery-dot-net-chapter-wise-interview-questions-7.8
Question 21:
Do all technologies support JSON?
Answer:
Yes, almost all technologies which deal with exchange of data support JSON. For instance if you want to that your WCF service should send JSON message rather than SOAP
you can set the “ResponseFormat” as “WebMessageFormat. Json” on your operation contract.

If you want your MVC to emit out JSON data you can return ” JsonResult” as shown below. If you call the below action it will emit out Customer objects in JSON format.

If you want to emit JSON using ASP.NET we need to use the “DataContractJsonSerializer” class as shown in the below code. “myPerson” is the class.

Question 22:
How can you make a JSON call using jQuery?
Answer:
Let’s assume you have a MVC controller action “getEmployee” which emits out employee JSON object as shown in the below code. Please note you can always emit JSON from any server technology like WCF, ASP.NET, MVC, etc., as discussed in the previous questions.

To make a call to the above MVC action using jQuery we need to use “getJSON” method. Below is the simple code for the same. It has three parameters:

  1. The first parameter is the URL which emits out JSON. For instance in the below code the URL is
    “/Employee/getEmployee”.
  2. The next parameter helps us to pass data to the resource which emits out JSON currently it’s the MVC action. The second parameter is NULL for now.
  3. The last parameter is the callback function which will be invoked once the MVC action returns data. You can see how the “getData” function just displays the “empcode” Because the output is in JSON it automatically converts the JSON data to JavaScript object.
Question 23:
How can we post JSON to Server?
Answer:
We can use the “post” method of jQuery to send data to the server. Below is how the post method call looks like. First parameter is the URL which will accept JSON data, second is the data which we want to send and the final parameter is the callback function where we receive the response.
Question 24:
How can we post a complete HTML form in JSON format?
Answer:
To post a complete HTML form we need to call “serialize” function as shown in the below code, “forml” is a HTML form. The data given by the function can then be passed to the “post” method of jQuery. “DisplayData” is a callback function to handle the output given by the server.
The above posted JSON string is received at the server side “request.inputstream”, below is a simple sample code for the same.
Question 25:
How can we convert JSON string into C# object?
Answer:
To convert a JSON string to a C# object we need to use the “JavascriptSerializer” class as shown in the below code.
“JsonString” is the string which has the JSON value and by using “Deserialize” we are converting the string to a C# object. Now this object which we receive is a collection of “key” and “value” pair which can be browsed and accessed in C#.
Question 26:
What are Single Page Applications (SPA)?
Answer:
SPA means your Web page has the following features:

  • Utilize the browser client power to the maximum by executing the maximum code on the client- side by using JavaScript, HTML and CSS.
  • Rather tnan loading the complete page necessary HTML fragments or JSON data is loaded as the user demands.
  • JavaScript which handles DOM manipulation, binding, Ajax calls are separated into controllers thus separating views and models.
  • DOM manipulations are replaced by declarative programming.

 

About the author

Scott

Leave a Comment