Safe ads

Sunday 26 March 2017

AngularJS Interview Questions Part - 2

AngularJS Interview Questions Part - 2



Q 1.     What are AngularJS Directives?
Ans 1. AngularJS directives are used to extend HTML. These are special attributes starting with ng- prefix.

Example – ng-app, ng-init, ng-model, ng-repeat, etc

Q 2.     How to use AngularJS ng-app directive?
Ans 2. This directive is used to auto-bootstrap an AngularJS application . The ng-app directive designates the root element of the page - e.g. on the <body> or <html> tags.

Example - <body ng-app="ngApp">

Q 3.    How to use AngularJS ng-init directive?
Ans 3. ng-init directive initializes an AngularJS application data. It is used to assign values to the variables to be used in the application.

Example - <body ng-app="ngApp" ng-init="CountryCapital_List = [['India', 'Delhi'], ['Bangladesh', 'Dhaka'], ['SriLanka', 'Colombo'], ['USA', 'Washington D.C.']]">

In the above example the variable CountryCapital_List is initialized to an array of arrays.


Q 4.     How to use AngularJS ng-model directive?
Ans 4. The ng-model directive binds an input, select, textarea (or custom form control) to a property on the model.

Example - <input type="text" ng-model="Name"/>

Q 5.     How to use AngularJS ng-repeat directive?
Ans 5. This directive repeats html elements for each item in a collection.

Example – <tr ng-repeat="Country in CountryCapital_List">

Q 6.     What is $watch() in AngularJS?
Ans 6. When you create a data binding from somewhere in your view to a variable on the $scope object, AngularJS creates a "watch" internally. A watch means that AngularJS watches changes in the variable on the $scope object. The framework is "watching" the variable. Watches are created using the  $scope.$watch() function.

The $scope.watch() function creates a watch of some variable. When you register a watch you pass two functions as parameters to the $watch() function:

·       A value function
·       A listener function

Example - $scope.$watch(function() {},
   function() {}
);

The first function is the value function and the second function is the listener function.

The value function should return the value which is being watched. AngularJS can then check the value returned against the value the watch function returned the last time. That way AngularJS can determine if the value has changed. Here is an example:

$.scope.$watch(function(scope)  {return scope.data.myVar },
                           function()  {}
);

This example value function returns the $scope variable scope.data.myVar. If the value of this variable changes, a different value will be returned, and AngularJS will call the listener function.

The listener function should do whatever it needs to do if the value has changed. Perhaps you need to change the content of another variable, or set the content of an HTML element or something. Here is an example:

$scope.$watch(function(scope)  { return scope.data.myVar },
                         function(newValue, oldValue)  {
                                document.getElementById(“”).innerHTML =
                                        “” + newValue + “”;
   }
);

This example sets the inner HTML of an HTML element to the new value of the variable.

Q 7.     What is $digest in AngularJS?
Ans 7. At key points in your application AngularJS calls the $scope.$digest() function. This function iterates through all watches and checks if any of the watched variables have changed. If a watched variable has changed, a corresponding listener function is called. The listener function does whatever work it needs to do, for instance changing an HTML text to reflect the new value of the watched variable. Thus, the $digest() function is what triggers the data binding to update.

The $scope.$digest() function iterates through all the watches in the $scope object, and its child $scope objects (if it has any). When $digest() iterates over the watches, it calls the value function for each watch. If the value returned by the value function is different than the value it returned the last time it was called, the listener function for that watch is called.
The $digest() function is called whenever AngularJS thinks it is necessary. For instance, after a button click handler has been executed, or after an AJAX call returns (after the done() / fail() callback function has been executed).
You may encounter some corner cases where AngularJS does not call the $digest() function for you. You will usually detect that by noticing that the data bindings do not update the displayed values. In that case, call $scope.$digest() and it should work.

Q 8.     What is $apply in AngularJS?
Ans 8. The $scope.$apply() function takes a function as parameter which is executed, and after that $scope.$digest() is called internally. That makes it easier for you to make sure that all watches are checked, and thus all data bindings refreshed. Here is an $apply() example:

$scope.$apply(function() {
        $scope.data.myVar = “New value”;
});

The function passed to the $apply() function as parameter will change the value of $scope.data.myVar. When the function exits AngularJS will call the $scope.$digest() function so all watches are checked for changes in the watched values.

Q 9.     What should be the maximum number of concurrent “watches”?
Ans 9. To reduce memory consumption and improve performance it is a good idea to limit the number of watches on a page to 2,000. A utility called ng-stats can help track your watch count and digest cycles.

Q 10.     What makes the angular.copy() method so powerful?
Ans 10. It creates a deep copy of the variable. A deep copy of a variable means it doesn’t point to the same memory reference as that variable. Usually assigning one variable to another creates a “shallow copy”, which makes the two variables point to the same memory reference. Therefore if we change one, the other changes as well.


Sunday 19 March 2017

AngularJS Interview Questions Part - 1

AngularJS Interview Questions Part - 1




Q 1.     What is AngularJS?
Ans 1. AngularJS is a powerful Javascript framework. It is used to create Single Page Application projects. It extends HTML DOM with additional attributes and makes it more responsive to user actions. It is licenced under the Apache license version 2.0. AngularJS is open source and completely free.

Q 2.     What are the advantages of using AngularJS?
Ans 2. Following are the advantages of using AngularJS –

·       Application written in AngularJS is cross-browser compliant and it automatically handles JavaScript code suitable for each browser.
·       AngularJS is a framework to build large scale and high performance web application while keeping them as easy-to-maintain.
·       Angular's data binding and dependency injection eliminate much of the code you currently have to write

Q 3.    What is the latest version of AngularJS framework?
Ans 3. The latest version of AngularJS framework is 1.6.3

Q 4.     What architectural pattern is followed by AngularJS?
Ans 4. AngularJS follows MVW architectural pattern i.e. Model View Whatever. It can act as MVC, MVVM and MVP. For several years AngularJS was closer to MVC, but over time and thanks to many refactoring and api improvements, it is now closer to MVVM – the $scope object could be considered the ViewModel that is being decorated by a function that we call a Controller. It is up to us to use AngularJS through any pattern that best suits our requirements.

Q 5.     What are the various features of AngularJS?
Ans 5. Following are the various Core features of AngularJS –

·       Data-binding 
·       Scope
·       Controller 
·       Services
·       Filters
·       Directives
·       Templates
·       Routing
·       Deep Linking
·       Dependency Injection

Q 6.     How to setup AngularJS environment?
Ans 6. You can setup the environment for AngularJS through various options-
1. You can download the AngularJS library directly from - https://angularjs.org/
2. You can use the AngularJS library directly from a google CDN
3. You can get it from GitHub.
Include the AngularJS framework in your code as follows –
<head>
   <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>


Q 7.     What are the disadvantages of AngularJS?
Ans 7. Though AngularJS has many advantages which make it an obvious choice for web development, but it still has some disadvantages -
·        Not Secure − Being JavaScript only framework, application written in AngularJS are not safe. Server side authentication and authorization is must to keep an application secure.
·        Not degradable − If your application user disables JavaScript then user will just see the basic page and nothing more.
Q 8.     What is data binding in AngularJS?
Ans 8. It is the automatic synchronization of data between model and view components. It is achieved through ng-model directive.

Q 9.     What is Scope in AngularJS?
Ans 9. It is an object that refers to the model. It acts as a glue between controller and view.

Q 10.     What is Controller in AngularJS?
Ans 10. Controller is a JavaScript function that is bound to a particular scope.

Click here to watch more informative AngularJS Tutorial videos!

Q 11.     What is Controller in AngularJS?
Ans 11. Controller is a JavaScript function that is bound to a particular scope.

Q 12.     What are services in AngularJS?
Ans 12. AngularJS comes with several built-in services for example $https: to make a XMLHttpRequests. These are singleton objects which are instantiated only once in app and can be used by different components.

Q 13.     What are filters in AngularJS?
Ans 13. These select a subset of items from an array and return a new array. These also format the data to be displayed.

Q 14.     What are directives in AngularJS?
Ans 14. Directives are markers on DOM elements (such as elements, attributes, css, and more). These can be used to create custom HTML tags that serve as new, custom widgets. AngularJS has built-in directives (ng-bind, ng-model and many more).

Q 15.     What are templates in AngularJS?
Ans 15. These are the rendered views with information from the controller and model. These can be a single file (like index.html) or multiple views in one page using "partials".

Q 16.     What is routing in AngularJS?
Ans 16. It is the concept of switching views.

Q 17.     What is deep linking in AngularJS?
Ans 17. Deep linking allows you to encode the state of application in the URL so that it can be bookmarked. The application can then be restored from the URL to the same state.

Q 18.     What is dependency injection in AngularJS?
Ans 18. AngularJS has a built-in dependency injection subsystem that helps the developer by making the application easier to develop, understand, and test.

Q 19.     Explain AngularJS boot process?
Ans 19. When the page is loaded in the browser, following things happen:

·        HTML document is loaded into the browser, and evaluated by the browser. AngularJS JavaScript file is loaded; the angular global object is created. Next, JavaScript which registers controller functions is executed.
·        Next AngularJS scans through the HTML to look for AngularJS apps and views. Once view is located, it connects that view to the corresponding controller function.
·        Next, AngularJS executes the controller functions. It then renders the views with data from the model populated by the controller. The page gets ready.



Friday 10 March 2017

JQuery Interview Questions Part - 8

JQuery Interview Questions Part - 8


JQuery Interview Questions



Q 1.     What are JQuery utilities?
Ans 1. JQuery provides several utility methods that are helpful in completing the desired programming tasks

Example –

Suppose, I have a string -  “     Hello       ” but I need the text “Hello” without any spaces on its left or right, then I can use trim() JQuery utility to remove any such spaces.

Q 2.     How to use JQuery trim utility method?
Ans 2. $.trim() is used to remove the leading and trailing whitespace.

Example –

$.trim(“     Hello       ”) will return “Hello”


Q 3.    How to use JQuery each utility method?
Ans 3. $.each() is used to iterate over arrays and objects. It can work on arrays and objects.

Example –

Usage with arrays –

$.each([“JQuery”, “Javascript”, “AngularJS”], function( index, value){
        alert(“Element at index: ” + index + “ is ” + value);
});

Result-
Element at index: 0 is JQuery
Element at index: 1 is Javascript
Element at index: 2 is AngularJS

Usage with Objects -
$.each({Library: “JQuery”, Framework: “AngularJS”}, function(key, value){
        alert(“Key: ” + key + “ value: ” + value);
});

Result-
Key: Library value: JQuery
Key: Framework value: AngularJS

Q 4.     How to use JQuery inArray utility method?
Ans 4. $.inArray() is used to Returns a value's index in an array, or -1 if the value is not in the array.

Example -

var testArray = [“JQuery”, “Javascript”,  “AngularJS”];

var returnedValue = $.inArray(“C++”, testArray);

alert(returnedValue);

Result: -1 (Because C++ does not exist in the array)

If “C++” is replaced with “AngularJS” in the above statement, then result will be: 2

Q 5.     How to use JQuery extend utility method?
Ans 5. $.extend() is used to change the properties of the first object using the properties of subsequent objects.

EXAMPLE –

var testObject1 = { Language: “C#”, Library: “JQuery”};

var testObject2 = { Language: “Java”, Library: “JQuery”};

var testObject3 = $.extend( testObject1, testObject2);

alert(testObject1.Language);

Result – C#

alert(testObject3.Language);

Result - Java

Q 6.     How to use JQuery proxy utility method?
Ans 6. $.proxy() is used to return a function that will always run in the provided scope i.e. it relates a general function to a specific object.

EXAMPLE –

var testFunction = function(){
alert(this);
}

var testObject = {Library: “JQuery”, Language: “C#”};

testFunction();

Result: [object Window]

var proxy = $.proxy(testFunction, testObject);

proxy();

Result: [object Object]




Friday 3 March 2017

JQuery Interview Questions Part - 7

JQuery Interview Questions Part - 7

 JQuery Interview Questions

 


Q 1.     What is AJAX?
Ans 1. AJAX is an acronym for Asynchronous JavaScript and XML. AJAX enables us to send and receive data asynchronously without reloading the web page. AJAX is used not only by many well known IT companies like Gmail, Google Maps, Youtube, Facebook but by almost every company nowadays.

Example –

Google was one of the first major companies to start using AJAX, and Google’s search suggestion tool was one of the first ways they used it, and one of the first auto-complete tools made. When typing into the Google search bar, it starts to use AJAX to get common results from the database on each keystroke.



Q 2.     What are the advantages of using AJAX?
Ans 2. (A) Response time is faster so increases performance and speed.
(B) Reduce the traffic travels between the client and the server.
(C) Page not required to be reloaded

Q 3.    How to use AJAX using JQuery?
Ans 3. JQuery offers many methods that enable us to use AJAX in an effective manner. You can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post and you can load the external data directly into the selected HTML elements of your web page!

Q 4.     How to use JQuery AJAX load method?
Ans 4. The load method loads data from a server and puts the returned data into the selected element.

SYNTAX - $(selector).load(URL, data, callback)
URL(required) – the URL where you want to load data from
data(optional) – It specifies a set of query string key/value pairs to send along with the request
callback(optional) – It is the name of the function to be executed after the load method is completed
Example – Suppose I have a text file(JQueryLoadTest.txt) on server having content as shown below –



If I need to load the HTML inside the above text file into a “<div>” element then use - $(“div”).load(“JQueryLoadTest.txt”);


Q 5.     How to use JQuery AJAX get method?
Ans 5. The get JQuery method requests data from the server with an HTTP GET request.

SYNTAX – $.get(URL, callback)
URL(required) – the URL you want to request
callback(optional) – It is the name of the function to be executed if the request is successful



EXAMPLE – Suppose you are using ASP.NET MVC project and you have an action method “GetString” inside a controller that returns a string “JQuery Tutorial” as shown below -

Now, if you want to get this string on button click using AJAX then use,

$(“button”).click(function(){
        $.get(ActionMethodURL, function(data){
                alert(data);
});
});

This would show an alert – “JQuery Tutorial”

Q 6.     How to use JQuery AJAX post method?
Ans 6. The post JQuery method requests data from the server with an HTTP POST request.

SYNTAX – $.post(URL, datacallback)

URL(required) – the URL you want to request
data(optional) – It specifies some data to send along with the request
callback(optional) – It is the name of the function to be executed if the request is successful


EXAMPLE – Suppose you are using ASP.NET MVC project and you have an action method “ChangeString” inside a controller that accepts a string and then returns it after changing it as shown below -

                       

You can Post a string to this action method and then get a changed string through JQuery POST method when user clicks a button  -

$(“button”).click(function(){
$.post(ActionMethodURL, ‘JQuery Tutorial’, function(data){
                alert(data);
});
});

We are posting the string “JQuery Tutorial” but we get the string – “Tech Geek” and it is alerted.

Q 7.    How to use JQuery ajax method?
Ans 7. The ajax JQuery method is used when some desired action cannot be obtained with other JQuery AJAX methods discussed before. For example, we want to specify what should happen in case an Ajax call fails.

SYNTAX – $.ajax(URL, [options])
URL(required) – the URL you want to request
options options is an object literal containing the configuration for the Ajax request.

EXAMPLE – You can now handle the successful posting of data as well as an error while doing so. With JQuery $.get() and $.post() methods you could not handle error scenario.


$(“button”).click(function(){
$.ajax(url:ActionMethodURL, method: “POST”, data: {Name:”Tech Geek”}, success:function(data){
                alert(“data posted successfully”);
},
error:function(JQxhr, exception){
        alert(“error occurred while posting data”);
});
});