Job and Interview Questions and Answers - (original) (raw)
Printing out the contents of an object in Javascript is quite beneficial when debugging your Javascript code. Following are some ways for printing out an object.
For/in loop of The Javascript
You can print out the contents of a Javascript object with the use of for/in loop. This loop is totally different when compared to a regular for loop. This is because the for/in loop is particularly used for iterating through the contents of an object. Below is an illustration of for/in loop’s general structure.
for( variable in object)
statement
While using the for/in loop, the actual object should be the string that comes after the “in”, thus whatever your object name is, you can place it there. And any arbitrary name can be given to the string that comes before the “in” operator.
Console.log to print out Javascript objects
In case, you do not want to use a Javascript alert because you think that alert boxes are irritating, then Console.log is another option that you can use for this procedure. It is a clean and simple way to print out the Javascript object’s contents, and it does not need you to iterate over each and every property like you have to do for the for/in loop. However, you must work with a console.
Now, a question comes in mind that what is a console? Well, you can think of it as the Javascript debugger. Whether an add-on or built-in, a console comes in with every major browser.
The most famous debugging tool is the Firebug, whereas IE, Chrome and Safari all have their own developer tools that will also display a Javascript console.
Printing out Javascript objects with JSON.stringify function
Another easy way of printing out the contents of a Javascript object is the utilization of the JSON.stringify function. JavaScript Object Notation (JSON) is actually a standard that is particularly used for data interchange. It is an amazing way of printing things out because there is no need to write out a for/in loop and you don’t have to use the console just for getting properties to print out. This is the most preferred method of printing out Javascript objects.
Although there is an absence of Black Scope in Javascript, however, it comprises of function scope. This term means that all the variables that are declared inside a function can be seen in the whole function in which they have been defined; it does not matter that those variables are declared if statement, for loop, inside a while loop, or some other block of code inside that function. The nested functions (which are actually the functions declared inside other functions) can even view and access variables that are declared in their enclosing, outer function(s). The best way to illustrate the function scope is by using an example.
- Function Scope in Javascript Example
function scopeTes() {
var x = 4;
//this will be true always:
If (x == 4)
{
var y = 30;
for (var i = 0; i <= 10; i++)
{
var inFor = i;
}
}
(z) console.log; // z is described, 30 prints
(j) console.log; // j is described, 12 prints
(inFor) console.log; // in For is described, 10 prints
}
Now from the above example, you can clearly see that the variables inFor, z and j are declared either inside the for loop or inside the if statement. However, those variables, are still visible to the remaining functions, even after being declared within those separate blocks. This is all because of those variables that are declared within one function. This is exactly what function scope is actually about.
Below is another demonstration that how Javascript can use function scope. In this example, a nested function can view and access variables that are declared in its enclosing, outer function.
- Nested Functions Can See Variables Declared In Their Enclosing Functions
ScopeTest function() {
variable x = 4;
/* however is a nested function
as it has been defined under
scopeTest
*/
function nested ( )
{
/* this is going to output 4 as the variable x is visible
although it has been declared under
scopeTest- the outer function */
alert(x);
}
// call to the nested function
nested();
}
// call to the outer function
scopeTest( );
In the above example, you can see that the nested function has access to the variable “x” of outer function. And because of this fact, the above code will output “4”. The reason behind this is that the Javascript uses function scope and here variables that are declared within a function can be seen even inside the nested functions.
All the PHP functions that begin using a double underscore are known as PHP magic functions. In the same way, the __autoload function is also known as a magic function. This is because it possesses a double underscore in front of it.
Significance of autoload function
The __autoload function is used in PHP for simplifying programmer’s job by incorporating classes in an automatic way. The programmer doesn’t need to add a very huge number of include statements. The below example will clarify this.
include “sample/sample.Foo.php”;
include “sample/sample.AB.php”;
include “sample/sample.XZ.php”;
include “sample/sample.YZ.php”;
$doo = new Doo;
$cd = new CD;
$xy = new XY;
$wy = new WY;
In the above code, you can see that we have included each of the class files one by one. This is due to the fact that we are creating each class’s instance, so we must have each class file. Of course, here we have assumed that developers are just defining one class each source file. This is in fact a good practice while you are writing object oriented programs, although you are permitted to have several classes in one source file.
Inclusion of Class Files Simplify the __Autoload Function in PHP
Suppose if we require using 20 or 30 different classes inside one file. Here, writing out every include statement could become problematic. The PHP __autoload function helps in solving this problem because it allows PHP to automatically load the classes for us! So, instead of the above code, we can simply use the __autoload function as illustrated below:
function __autoload($sample_name)
{
need_once “./class/class.”.$class_name.“.php”;
}
$doo = new Doo;
$cd = new CD;
$xy = new XY;
$wy = new WY;
Working Of The __Autoload Function
Since the __autoload function is a magic function, thus the programmer does not need to call it directly. Rather, it is called by PHP behind the scenes– and this is the feature that makes it magical. So, when does the __autoload function truly get called? Here, in the above code, the __autoload function is going to be called 4 times, because PHP will not be able to recognize the Doo, CD, XY, and WY classes so PHP will surely make a call to the __autoload function every time when it does not identify a class name.
Before we start to discuss that what actually a namespace is, it would be best to illustrate a simple example of why and when we would require namespaces. Have a look at the following two fictional C++ header files:
// anyheaderfile.h
class Sample { … };
// anyslibfile.h
class Sample { … };
Notice that it would not be possible to use both of the above header files in one program. This is because there are 2 classes having completely same name – “Sample”. Obviously, if we try to use the “Sample” class after incorporating both of the header files, then it would not be evident that which Sample class we are referring to – the one in the anyheaderfile.h file or the one in the anylibfile.h file.
In this case, namespaces can help. First think of namespace as a declarative region that imposes the attachment of an additional identifier to any names asserted inside that region. Since this is the formal definition, so it might be confusing for you. Another example will help in clarifying the concept.
Just memorize that if the “Sample” classes that we displayed above are in different namespaces, then every name will be distinct. This is just due to the namespace identifier’s addition. The following is an example of namespaces in action.
C++ Namespace Example
// anyheaderfile.h
namespaceanyHeader
{
class Sample { … };
}
// anylibfile.h
namespaceAnyLib
{
class Sample { … };
}
In the above example, we have namespaces and we can call every“Sample” classes without getting confused that which “Sample” class we are referring– the one in the AnyLib file or the one in the AnyHeader file. In other terms, the class names are not going to clash. However, how do we in reality make those calls utilizing the correct syntax?
Well, here we can make use of the scope resolution operator. You must have seen “::”before in C++. Thus, to access each of those classes utilizing their concerning namespace, it would appear like this:
AnyHeader::Sample
AnyLib::Sample
Therefore, using the namespace allows us to be more “particular” with regards to which Sample class we want to reference. And there is no name clashing and no ambiguity.
In Java, every variable has a type, which basically tells Java about the treatment of that variable, and how much memory should be assigned for that variable. Generally, Java has key types of characters, different types of floating point numbers, different types of integers, and also types for the values false and true –bool, char, float and int. All these basic kinds are named as primitive types.
What is a class type in Java?
As you probably already know, Java classes are created for solving object oriented problems. Thus, any object of a class has the “class” type. Since an object of a class is more complicated as compared to a simple boolean, integer or other primitive type, therefore, a variable naming an object is identified to be a class type.
Reference types vs. Object types vs. Class types
You may be puzzled by all these different terminologies. Thus, just to clarify, reference types, class types and object types all are the same thing having the same meaning (an object of a class).
Differences Between Primitive And Class Types In Java
In Java, a variable of a class type – such as a String – stocks objects of its class in a different manner of how variables of primitive types – such as char or int – store their values. Each variable, whether it’s of a class type or a primitive type, is implemented as a location in the memory of the computer.
For a primitive type variable, the variable’s value is stored in the memory location allocated to the variable. Thus, in case, an integer variable is stated as “int x = 2″, then when we see the memory location of “x”, there will be a “2” stored there as per the expectation.
But, a variable of a class type stocks the memory address about the location of the object as well as the values inside the object. Thus, in case, we have a class named “FewClass”, when we develop an object like this: “FewClass anObject”, then in memory when we see at “anObject”, we will find out that it does not stock any of the variables that is related to that object in memory.
Rather “anObject” variable only stocks an address of another place in memory where all the description of “anObject” exists. This clearly means that the variable comprises of just the memory address of where the object is stored, which is also known as a reference to the object.
In Java, anonymous classes are more precisely known as anonymous inner classes. Always remember, without the “inner”, there’s no such thing as anonymous classes. This distinction is significant because anonymous inner classes mean that they are defined within another class.
Basically, anonymous inner classes are those inner classes that are declared without using any class name at all, and due to this fact, they are called anonymous classes. Beside this, anonymous inner classes also have some pretty unusual syntax.
Here is an example with a few codes of an anonymous inner class.
Class DeveloperTest{
public void read() {
System.out.println(“DeveloperTest!”);
}
}
classForum{
/* This creates an anonymous inner class: */
DeveloperTest dInstance = new DeveloperTest () {
public void read() {
System.out.println(“anonymous DeveloperTest “);
}
};
}
In the above code, you can clearly see that there are 2 classes – one called Forum and another called DeveloperTest. The DeveloperTest class is pretty straightforward – there’s just an easy procedure called “read()” that prints the text “DeveloperTest!” when called.
However, the code that you really need to look carefully at is inside the Forum class, which is highlighted in the color green. You may think that we are creating an instance of the DeveloperTest class called dInstance in that code, however, what’s really happening in that there is creation of an instance of an anonymous class.
In the above code, we have created an instance of a subclass, also called as a child class of the DeveloperTest class. Here the most critical thing to understand is that this instance (dInstance) is actually an instance of an anonymous child class of the DeveloperTest class.
An anonymous inner class- Reason behind the name
The major reason that why it’s known an anonymous inner class is that the class that we have now created is without any name! We created an instance of the class, however, we did not emphasize to give a name to the class. We just have a reference variable (in the above example, it is dInstance) for the anonymous inner class.
Both prepared statements and parameterized queries are exactly the same thing. Basically, the word ‘prepared statement’ is most frequently used; however, there is no difference between these two terms.
Prepared statements and parameterized queries are database management system’s characteristics that actually act as templates in which the execution of SQL occurs. The parameters are the actual values that pass through into the SQL. This is why all these templates are known as parameterized queries. Moreover, the SQL within the template is also optimized, parsed and compiled before the SQL is sent off for the execution– in other words “prepared”. Therefore, all these templates are generally also known as prepared statements. Thus, always remember that prepared statements and parameterized queries are the two names given to the same thing.
Benefits Of Using Prepared Statements
The 2 main advantages provided by the prepared statements are as follows.
First of all, they provide efficient performance. Although, the execution of a prepared statement can happen several times, it is optimized and compiled just once using the database engine.
Due to the fact that a prepared statement does not need to be optimized and compiled each and every time there is a change in the values of the query, it offers a definite performance advantage. However, remember that when a prepared statement is compiled, all the query optimization cannot occur.
This reason behind this is that the best query plan may also rely on the certain values of the parameters being passed in. Overtime, the best query plan may also change due to the fact that the indices and database tables also change with time.
How SQL Is “Prepared”?
- The creation of prepared SQL requires calling the respective prepare procedure in each language.
- After this, the prepared SQL template is forwarded to the DBMS (it can be DB2, MySQL or whatever) having “?”(The placeholder values) left blank.
- Now, the DBMS will perform, parse and compile query optimization on the template.
- After this, the DBMS will store the result; however, it cannot execute the result as it does not have any values for the execution as there is no data in the parameters/placeholders.
- Once the data is passed in for the parameters and respective execute function is called, the SQL is executed.
The inner classes were first introduced in Java 1.1 version. Since the introduction of inner classes, they have inspired several different opinions among people. It is essential to know that when you can use inner classes because the utilization of inner classes in the wrong conditions can result in code that’s not easy to maintain and understand.
An Example Of Where Inner Classes Are Important
Inner classes basically allow you to define one class with another class. This is why they are known as “inner” classes. Below is an example to illustrate that when inner classes are essential.
Suppose you have a Java Graphical User Interface (GUI) class that serves as a chat client, such as Facebook Chat or Gchat. Now think about the methods require to be presented in a class similar to that which shows a chat client. Here you will require a method that is going to read user input from the chat box, for actually sending the user input to whomsoever the user is chatting with and to the server.
Event handlers
However, one important thing is missing here. How exactly will those procedures be called? So, think about the working of Gchat as an example– Suppose, you just type in some text and ready to press the “Return” or “Enter” key on your keyboard to send it to whomsoever you’re chatting with.
Therefore, the “Enter” key could be regarded as one event that activates a call to the client procedures. And, in case, the chat client class is looking to detect whether someone is typing in a window, then this event will clearly activate the call that someone is typing. Thus, we require some code for detecting when someone is actually typing in the window in real time – essentially when they are pressing a button within their chat window.
Hence, all these events – such as, typing in a window and then pressing the “RETURN” key also needs some procedures for detecting when they occur, and those event handling procedures can then call the suitable chat client procedures.
The answer of this question can be best explained with an example. Just suppose we have the following SQL table having the columns printerrModel and modelNumber:
Computer {
modelNumber CHAR(20) NOT NULL,
printerModelCHAR(10),
}
Now assume that the table stores entries for all the ‘makes’ of printers and PC’s – and in case, it’s a printer the printerModel field is set. With the available information, let’s try to answer a question for explaining that what a three valued logic is:
How will you write a SQL statement that returns just the PC’s and no printers from the above table?
You might be thinking that the answer of this question is very simple, and the first thing that may come to mind is the following answer:
SELECT * FROM Computer WHERE printerModel = null
- Three valued logic/Ternary used by SQL
Basically, the above SQL code is not going to return anything at all – which also include the PC’s that are actually present in the table! The reason behind this is the fact that the SQL uses three-valued logic or ternary. The ternary logic concept is significant to understand for writing effective SQL queries.
- SQL Logical Operations using 3 possible values
An important fact to remember is: There are 3 possible values in SQL logical operations NOT 2. But, the question arises, what are those 3 possible values? They are UNKNOWN, TRUE and FALSE. The UNKNOWN value, like its name shows, simply means that a value which is unrepresentable or unknown. If we will run the above presented SQL code, this will return UNKNOWN for a value.
- The Equality Operator
The issue with the above SQL statement is the fact that we used the “=” (equality operator) in order to test a NULL column value. A comparison to NULL returns UNKNOWN in the majority of databases. This is correct even when matching NULL with NULL. The appropriate way to check for a non-NULL column or NULL is to use the IS NOT NULL or the IS NULL syntax. Thus, the SQL query must be changed to the following:
SELECT * FROM Computer WHERE printerModel IS NULL
A full table scan, scans one by one all the rows present in a table for finding the data that a query is searching for. Apparently, this results in very slow SQL queries when you have a table having multiple rows.
Now you can even imagine that how performance-rigorous a complete table scan would be on a table having millions of rows. However, you can help prevent full table scans by using an index. Following are a few scenarios which result in a complete table scan:
When Statistics Are Not Updated
Generally, statistics are kept on indexes and tables. However, a full table scan will happen, if for any reason index or table statistics have not been updated. The major reason behind this is that most RDBMS’s comprises of query optimizers that utilize those statistics for figuring out whether using an index is useful. And when those statistics are not present, then the RDBMS may mistakenly determine that doing a complete table scan is more beneficial as compared to using an index.
A full table scan might be performed, in case; there is no WHERE clause in a query for filtering out the rows which are shown in the result set.
Full Table Scan Using An Index
There are a few scenarios in which a complete table scan will take place even in the presence of an index on that table, such as:
A full table scan will be performed when a query does have a WHERE clause, however, not a single column in that WHERE clause is identical to the index’s leading column on the table.
A full table scan can still occur even if a query comprises of a WHERE clause having a column that is identical to the index’s first column. Generally, this situation arises when the use of an index is prevented because comparison is being used by the WHERE clause. Following are some scenarios in which that could happen:
- When the wildcard operator is utilized in the comparison string’s first position.
- When the NOT operator is used.
- When the “<>“(NOT EQUAL) operator is used.