Saturday, October 19, 2013

jQuery interview questions and answers



Questions 1: When Jquery founded and by whome?
Answers 1: It was released in January 2006 at BarCamp NYC by John Resig
(Jquery founder).


Questions 2: What is jQuery?
Answers 2: jQuery is fast, lightweight and feature-rich client side JavaScript 
Library/Framework which helps in to traverse HTML DOM, make animations, 
add Ajax interaction, manipulate the page content, change the style and provide 
cool UI effect. It is one of the most popular client side library and as per 
a survey it runs on every second website.


Questions 3: Why do we use jQuery?
Answers 3: 
Easy to use and learn.
Easily expandable.
Cross-browser support (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)
Easy to use for DOM manipulation and traversal.
Large pool of built in methods.
AJAX Capabilities.
Methods for changing or applying CSS, creating animations.
Event detection and handling.
Tons of plug-ins for all kind of needs.


Questions 4: How JavaScript and jQuery are different?
Answer 4: JavaScript is a language While jQuery is a library built in the 
JavaScript language that helps to use the JavaScript language.


Questions 5: Is jQuery replacement of Java Script?
Answer 5: No. jQuery is not a replacement of JavaScript. 
jQuery is a different library which is written on top of JavaScript. 
jQuery is a lightweight JavaScript library that emphasizes interaction between 
JavaScript and HTML.


Questions 6: Is jQuery a library for client scripting or server scripting?
Answers 6: Client side scripting.


Questions 7: What is the basic need to start with jQuery? 
Answers 7: To start with jQuery, one need to make reference of it's library.
The latest version of jQuery can be downloaded from 
jQuery.com.


Questions 8: Which is the starting point of code execution in jQuery? 
Answers 8: The starting point of jQuery code execution is 
$(document).ready() function which is executed when DOM is loaded.


Questions 9: What does dollar sign ($) means in jQuery? 
Answers 9: Dollar Sign is nothing but it's an alias for JQuery. 
Take a look at below jQuery code.
$(document).ready(function(){
});
jQuery(document).ready(function(){
});
Over here $ sign can be replaced with "jQuery" keyword. Questions 10: Can we have multiple document.ready() function on the same page? Answers 10: YES. We can have any number of document.ready() function on the same page. Questions 11: Can we use our own specific character in the place of $ sign in jQuery? Answers 11: YES. It is possible using jQuery.noConflict(). Questions 12: What is jQuery.noConflict? Answers 12: As other client side libraries like MooTools, Prototype can be used with jQuery and they also use $() as their global function and to define variables. This situation creates conflict as $() is used by jQuery and other library as their global function. To overcome from such situations, jQuery has introduced jQuery.noConflict().
jQuery.noConflict();
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
   jQuery("div").hide();
});  
You can also use your own specific character in the place of $ sign in jQuery.
var $j = jQuery.noConflict();
// Use jQuery via jQuery(...)
$j(document).ready(function(){
   $j("div").hide();
}); 
Questions 13: Is there any difference between body onload() and document.ready() function? Answers 13: document.ready() function is different from body onload() function for 2 reasons. 1) We can have more than one document.ready() function in a page where we can have only one body onload function. 2) document.ready() function is called as soon as DOM is loaded where body.onload() function is called when everything gets loaded on the page that includes DOM, images and all associated resources of the page. Questions 14: What is the difference between .js and .min.js? Answer 14: Query library comes in 2 different versions Production and Deployment. The deployment version is also known as minified version. So .min.js is basically the minified version of jQuery library file. Both the files are same as far as functionality is concerned but .min.js is quite small in size so it loads quickly and saves bandwidth. Questions 15: What is jQuery Selectors? What are selectors in jQuery and how many types of selectors are there? Answers 15: Selectors are used in jQuery to find out DOM elements. Selectors can find the elements via ID, CSS, Element name and hierarchical position of the element. Name: Selects all elements which match with the given element Name. #ID: Selects a single element which matches with the given ID .Class: Selects all elements which match with the given Class. Universal (*): Selects all elements available in a DOM. Multiple Elements E, F, G: Selects the combined results of all the specified selectors E, F or G. Attribute Selector: Select elements based on its attribute value. Questions 16: What does $("div") will select? Answer 16: his will select all the div elements on page. Questions 17: What does $("div.parent") will select? Answer 17: All the div element with parent class. Questions 18: What are the fastest selectors in jQuery? Answer 18: ID and element selectors are the fastest selectors in jQuery. Questions 19: What are the slow selectors in jQuery? Answers 19: class selectors are the slow compare to ID and element. Questions 20: Which one is faster Jquery ID selector or JavaScript getElementById()? Answer 20: JavaScript getElementById() is faster than Jquery Id ($("#elementID")) selector Questions 21: Where Jquery code execute? On client browser or server browser? Answer 21: On client browser Questions 22: Difference between $(this) and 'this' in jQuery? Answers 22: this and $(this) refers to the same element. The only difference is the way they are used. 'this' is used in traditional sense, when 'this' is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.
$(document).ready(function(){
    $('#spnValue').mouseover(function(){
       alert($(this).text());
  });
});
In below example, this is an object but since it is not wrapped in $(), we can't use jQuery method and use the native JavaScript to get the value of span element.
$(document).ready(function(){
    $('#spnValue').mouseover(function(){
       alert(this.innerText);
  });
});
Questions 23: How do you check if an element is empty? Answer 23: There are 2 ways to check if element is empty or not. We can check using ":empty" selector.
$(document).ready(function(){
    if ($('#element').is(':empty')){
       //Element is empty
  }
});
And the second way is using the "$.trim()" method.
$(document).ready(function(){
    if($.trim($('#element').html())=='') {
       //Element is empty
  }
});
Questions 24: How do you check if an element exists or not in jQuery? Answer 24:
$(document).ready(function(){
    if ($('#element').length > 0){
       //Element exists
  });
});
Questions 25: What is the use of jquery .each() function? Answer 25: The $.each() function is used to iterate over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array. Questions 26: What is the difference between jquery.size() and jquery.length? Answer 26: jQuery .size() method returns number of element in the object. But it is not preferred to use the size() method as jQuery provide .length property and which does the same thing. But the .length property is preferred because it does not have the overhead of a function call. Questions 27: What is the difference between $('div') and $('<div/>') in jQuery? Answers 27: $('<div/>') : This creates a new div element. However this is not added to DOM tree unless you don't append it to any DOM element. $('div') : This selects all the div element present on the page. Questions 28: What is the difference between parent() and parents() methods in jQuery? Answers 28: The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree. Questions 29: How To update info already in a table and Delete a row(s) from a table? Answer 29: mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y', Update_priv = 'Y' where [field name] = 'user'; mysql> DELETE from [table name] where [field name] = 'whatever'; Questions 30: Explain .bind() vs .live() vs .delegate() vs .on() Answer 30: All these 4 jQuery methods are used for attaching events to selectors or elements. But they all are different from each other. .bind(): This is the easiest and quick method to bind events. But the issue with bind() is that it doesn't work for elements added dynamically that matches the same selector. bind() only attach events to the current elements not future element. Above that it also has performance issues when dealing with a large selection. .live(): This method overcomes the disadvantage of bind(). It works for dynamically added elements or future elements. Because of its poor performance on large pages, this method is deprecated as of jQuery 1.7 and you should stop using it. Chaining is not properly supported using this method. .delegate(): The .delegate() method behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored and it also supports chaining. .on(): Since live was deprecated with 1.7, so new method was introduced named ".on()". This method provides all the goodness of previous 3 methods and it brings uniformity for attaching event handlers. Questions 31: How to create clone of any object using jQuery? Answer 31: jQuery provides clone() method which performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes.
$(document).ready(function(){
  $('#btnClone').click(function(){
     $('#dvText').clone().appendTo('body');
     return false;
  });
});
Questions 32: What is difference between prop and attr? Answer 32: attr(): Get the value of an attribute for the first element in the set of matched elements. Whereas,.prop(): (Introduced in jQuery 1.6) Get the value of a property for the first element in the set of matched elements. attr() gives you the value of element as it was defines in the html on page load. It is always recommended to use prop() to get values of elements which is modified via javascript/jquery , as it gives you the original value of an element's current state. Questions 33: What is event.PreventDefault? Answer 33: The event.preventDefault() method stops the default action of an element from happening. Questions 34: What is the difference between event.PreventDefault and "return false"? Answer 34: e.preventDefault() will prevent the default event from occurring, e.stopPropagation() will prevent the event from bubbling up and return false will do both. Questions 35: How to check if number is numeric while using jQuery 1.7+? Answer 35: Using "isNumeric()" function which was introduced with jQuery 1.7. Questions 36: How to check data type of any variable in jQuery? Answer 36: Using $.type(Object) which returns the built-in JavaScript type for object. Questions 37: How do you attach a event to element which should be executed only once? Answer 37: Using jQuery one() method. This attaches a handler to an event for the element. The handler is executed at most once per element. In simple terms, the attached function will be called only once.
$(document).ready(function() {
    $("#btnDummy").one("click", function() {
        alert("This will be displayed only once.");
    });
});​
Questions 38: Can you include multiple version of jQuery? Answer 38: Yes. Multiple versions of jQuery can be included in same page. Below code shows how to include multiple version of jQuery.





By this way, for your own jQuery code use "$jq", instead of "$" as "$jq" refers to jQuery 1.9.1, where "$" refers to 1.7.2. Questions 39: Can we use jQuery to make ajax request? Answer 39: Yes. jQuery can be used for making ajax request. Questions 40: What are various methods to make ajax request in jQuery? Answers 40: Using below jQuery methods, you can make ajax calls. load() : Load a piece of html into a container DOM $.getJSON(): Load JSON with GET method. $.getScript(): Load a JavaScript file. $.get(): Use to make a GET call and play extensively with the response. $.post(): Use to make a POST call and don't want to load the response to some container DOM. $.ajax(): Use this to do something on XHR failures, or to specify ajax options (e.g. cache: true) on the fly.

Monday, October 14, 2013

Mysql interview questions and answers


Questions 1:  how to do login in mysql with unix shell?
Answers 1: By below method if password is pass and user name is root
           # [mysql dir]/bin/mysql -h hostname -u root -p pass


Questions 2: how you will Create a database on the mysql server with 
unix shell?
Answers 2: mysql> create database databasename;


Questions 3: How Switch (select or use) to a database?
Answer 3: mysql> use databasename;


Questions 4: How To see all the tables from a database of mysql server?
Answer 4: mysql> show tables;


Questions 5: How to see table's field formats or description of table?
Answers 5: mysql> describe tablename;


Questions 6: How to delete a database from mysql server?
Answers 6: mysql> drop database databasename;


Questions 7: How to delete a table? 
Answers 7: mysql> drop table tablename;


Questions 8: How we get Sum of column? 
Answers 8: mysql> SELECT SUM(*) FROM [table name];


Questions 9: how to list or view all databases from the mysql server.? 
Answers 9: mysql> show databases;


Questions 10: How you will Show all data from a table? 
Answers 10: How you will Show all data from a table.

Questions 11: How to returns the columns and column information pertaining 
to the designated table?
Answers 11: mysql> show columns from tablename;


Questions 12: How to Show certain selected rows with the value "dh"?
Answers 12: mysql> SELECT * FROM tablename WHERE fieldname = "dh";
   

Questions 13: How will Show all records containing the name "sonia" AND 
the phone number '1234567890'?
Answers 13: mysql> SELECT * FROM tablename WHERE name = "sonia" AND 
phone_number = '1234567890';
   

Questions 14: How you will Show all records not containing the name "sonia" 
AND the phone number '1234567890' order by the phone_number field?
Answer 14: mysql> SELECT * FROM tablename WHERE name != "sonia" AND 
phone_number = '1234567890' order by phone_number;
   

Questions 15: How to Show all records starting with the letters 'sonia' 
AND the phone number '1234567890'?
Answers 15: mysql> SELECT * FROM tablename WHERE name like "sonia%" 
AND phone_number = '1234567890';
   

Questions 16: Use a regular expression to find records. Use "REGEXP BINARY" 
to force case-sensitivity. This finds any record beginning with r?
Answer 16: mysql> SELECT * FROM tablename WHERE rec RLIKE "^r";
   

Questions 17: How you will Show unique records?
Answer 17: mysql> SELECT DISTINCT columnname FROM tablename;
   

Questions 18: how we will Show selected records sorted in an ascending (asc) 
or descending (desc)?
Answer 18: mysql> SELECT col1,col2 FROM tablename ORDER BY col2 DESC;
mysql> SELECT col1,col2 FROM tablename ORDER BY col2 ASC;
   

Questions 19: how to Return total number of rows?
Answers 19: mysql> SELECT COUNT(*) FROM tablename;
   

Questions 20: How to Join tables on common columns?
Answer 20: mysql> select lookup.illustrationid, lookup.personid,person.birthday from 
lookup left join person on lookup.personid=person.personid=statement to join birthday 
in person table with primary illustration id
   

Questions 21: How to Creating a new user. Login as root. Switch to the MySQL 
db. Make the user. Update privs?
Answer 21: # mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) 
VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;


Questions 22: How to Change a users password from unix shell?
Answers 22: # [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p 
password 'new-password'
   

Questions 23: How to Change a users password from MySQL prompt. Login as 
root. Set the password. Update privs?
Answer 23: # mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;
   

Questions 24: How to Recover a MySQL root password. Stop the MySQL server 
process. Start again with no grant tables. Login to MySQL as root. 
Set new password. Exit MySQL and restart MySQL server?
Answer 24: 
# /etc/init.d/mysql stop 
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql>update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
   

Questions 25: How to Set a root password if there is on root password?
Answer 25: # mysqladmin -u root password newpassword
   

Questions 26: How to Update a root password?
Answer 26: # mysqladmin -u root -p oldpassword newpassword
   

Questions 27: How to allow the user "sonia" to connect to the server from 
localhost using the password "passwd". Login as root. Switch to the MySQL 
db. Give privs. Update privs?
Answers 27: # mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to sonia@localhost identified by 'passwd';
mysql> flush privileges;
   

Questions 28: How to give user privilages for a db. Login as root. Switch to the 
MySQL db. Grant privs. Update privs?
Answers 28: # mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,Db,User,Select_priv,Insert_priv,Update_priv,
Delete_priv,Create_priv,Drop_priv) VALUES 
('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges; 
or 
mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;
   

Questions 29: How To update info already in a table and Delete a row(s) from 
a table?
Answer 29: 
mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',
Update_priv = 'Y' where [field name] = 'user'; 
mysql> DELETE from [table name] where [field name] = 'whatever';
   

Questions 30: How to Update database permissions/privilages?
Answer 30: mysql> flush privileges;
   

Questions 31: How to Delete a column and Add a new column to database?
Answer 31: mysql> alter table [table name] drop column [column name];
mysql> alter table [table name] add column [new column name] varchar (20);
   

Questions 32: Change column name and Make a unique column so we get 
no dupes?
Answer 32: 
mysql> alter table [table name] change [old column name] 
[new column name] varchar (50);
mysql> alter table [table name] add unique ([column name]);
   

Questions 33: How to make a column bigger and Delete unique from table?
Answer 33: mysql> alter table [table name] modify [column name] VARCHAR(3);
mysql> alter table [table name] drop index [colmn name];
   

Questions 34: How to Load a CSV file into a table?
Answer 34: 
mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE 
[table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' 
(field1,field2,field3);
   

Questions 35: How to dump all databases for backup. Backup file is sql 
commands to recreate all db's?
Answer 35: 
# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
   

Questions 36: How to dump one database for backup?
Answer 36: # [mysql dir]/bin/mysqldump -u username -ppassword --databases 
            databasename >/tmp/databasename.sql
   

Questions 37: How to dump a table from a database?
Answer 37: # [mysql dir]/bin/mysqldump -c -u username -ppassword databasename 
            tablename > /tmp/databasename.tablename.sql
   

Questions 38: Restore database (or database table) from backup?
Answer 38: # [mysql dir]/bin/mysql -u username -ppassword databasename < 
            /tmp/databasename.sql
   

Questions 39: How to Create Table show Example?
Answer 39: 
mysql> CREATE TABLE [table name] (firstname VARCHAR(20),
middleinitial VARCHAR(3), lastname VARCHAR(35),
suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),
username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), 
groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));


Questions 40: How to search second maximum(second highest) salary 
value(integer)from table employee (field salary)in the manner so that 
mysql gets less load?
Answers 40: By below query we will get second maximum(second highest) 
salary value(integer)from table employee (field salary)in the manner so 
that mysql gets less load 

SELECT DISTINCT(salary) FROM employee order by salary desc limit 1 , 1 ; 

(This way we will able to find out 3rd highest , 4th highest salary so on 
just need to change limit condtion like LIMIT 2,1 for 3rd highest and 
LIMIT 3,1 for 4th 
some one may finding this way using below query that taken more time 
as compare to above query.

SELECT salary FROM employee where salary < (select max(salary) from employee) 
order by salary DESC limit 1 ;