Friday, 31 October 2008

Java concepts

4.  JVM with version
version 4 - Autoboxing (wrapping primitive to objects automaticaly) and Unboxing
Version 5 - Generic/Templating, Iterator, You can create array by using triple dot ...
  - static import,  import static 
Version 6 - No Idea yet
Version 7 - Closure(Functions inside functions and functions will have a reference)

7. Serialization (Usages)
1. persistence 
2. copy mechanism
3. communication mechanism

8. Cloning: 
Deep and sallow cloning:

Thought: while doing deep cloning, copy of the object might we can not copy all the objects so easly,  so serilize it and deserilize it
   How to serilize and deserialize and object?
   Sample code:
Contact  myObject = new Contact();
System.out.println("Previous: "+myObject);
ByteArrayOutputStream fos=new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(fos);
byte[] array=fos.toByteArray();
ByteArrayInputStream fis=new ByteArrayInputStream(array);
ObjectInputStream ois = new ObjectInputStream(fis);
Contact myDeserializedObject = (Contact)ois.readObject();
System.out.println("After: "+myDeserializedObject);

9. How to make a class immutable?
1. make all variable private
2. no setter methods
3. in getter method, two things should be there
1. either we return the copy of the class variables
2. or we return a an object of class which is immutable again, like string
4. in constructor also we don't assign class variable directly to passed variables, it should copy the content

Sample questions on String immutability
String s1="abc";
String s2="abc";
s1.equals(s2), true
s1==s2, true
String s3=new String("abc");
s1.equals(s3), true
s1==s3, false

10. Why do we need to extends RuntimeException class?
Answer is consistency in approach, 
There are two basic type of exception, application exception(checked) and system exceptions (unchecked) So when we feel that some exception need not to reach to use, we'd like to wrap that in a system exception. There should be two base Exception classes, one for each and should be use whenever relevent exceptions are thrown. When SQLException is thrown, we wrap it in a custom system exception and throw it, note: checked is been converted in to unchecked.

11. Can we declare a class as static?
Top level calsses can not be declared as static, compiler error.
Inner classes can be, and it act as a static object of outer class. Static inner classes imply that the object of the inner class is not dependent upon the outer class object and can exist indepenently.
Now there are four types of Inner classes.
1. Anonymous: since Anonymous classes have not got proper class declration,     no possibility of making it static
2. Local : Local classes are the same as local variables, local classes aren't allowed to be declared public, protected, private, or static.
3. Member: The member class is the only class that you can declare static. When you declare a member class, you can instantiate that member class only within the context of an object of the outer class in which this member class is declared. If you want to remove this restriction, you declare the member class a static class.
4. Nested top-level. A nested top-level class is a member classes with a static modifier. A nested top-level class is just like any other top-level class except that it is declared within another class or interface.


Four JDBC driver types.

Type 1: JDBC-ODBC Bridge plus ODBC Driver:
The first type of JDBC driver is the JDBC-ODBC Bridge. It is a driver that provides JDBC access to databases through ODBC drivers. The ODBC driver must be configured on the client for the bridge to work. This driver type is commonly used for prototyping or when there is no JDBC driver available for a particular DBMS.

Type 2: Native-API partly-Java Driver:
The Native to API driver converts JDBC commands to DBMS-specific native calls. This is much like the restriction of Type 1 drivers. The client must have some binary code loaded on its machine. These drivers do have an advantage over Type 1 drivers because they interface directly with the database.

Type 3: JDBC-Net Pure Java Driver:
The JDBC-Net drivers are a three-tier solution. This type of driver translates JDBC calls into a database-independent network protocol that is sent to a middleware server. This server then translates this DBMS-independent protocol into a DBMS-specific protocol, which is sent
to a particular database. The results are then routed back through the middleware server and sent back to the client. This type of solution makes it possible to implement a pure Java client. It also makes it possible to swap databases without affecting the client.

Type 4: Native-Protocol Pur Java Driver
These are pure Java drivers that communicate directly with the vendor’s database. They do this by converting JDBC commands directly into the database engine’s native protocol. This driver has no additional translation or middleware layer, which improves performance tremendously.

1. Is there any limitation for no of statments executed with in batchupdate?

No, any number of statements we can write in the batch update but all the updates only means (insert,delete,update) not select

2. What happens when we execute "Class.forname("Driver class name");"

By the end of the execution of Class.forName("Driver
class"); the driver class should be loaded into the memory
but also
1. The driver class should be initialized
2. Should be registered with the driver manager class

The above two operations are not done by forName() . So a pure Static() block is defined in which the above two tasks are manipulated and by which we are able to get connection, immediately after loading the driver class without writing any code to initialize the driver class.

3. What are the Statements in JDBC?

1. Statement: Each time you fire a query it compile and executes and returns the result set
2. PreparedStatement: It parses, compile and optimize the query and keep it, next time when query fired, it picks the compiled query and excute. Also you send dynamic sql statements.
3. Callable Statement: To execute stored procedure like pl/sql

Database concepts

1. Triggers
In a DBMS, a trigger is a SQL procedure that initiates an action (i.e., fires an action) when an event (INSERT, DELETE or UPDATE) occurs. Since triggers are event-driven specialized procedures, they are stored in and managed by the DBMS. A trigger cannot be called or executed; the DBMS automatically fires the trigger as a result of a data modification to the associated table. Triggers are used to maintain the referential integrity of data by changing the data in a systematic fashion.

2. Index Covering
A nonclustered index that includes (or covers) all columns used in a query is called a covering index. Index covering means that “Data can be found only using indexes, without touching the tables”.


At the very first JOIN means catesian product of two tables and join can take some codition to filter out the results.
As example consider two table and a sample query
TableA(id, name)
TableB(id, address)

select * from TableA ta $1 TableB tb on

1. if $1=INNER JOIN
JOIN is equivalent to INNER JOIN
Records will be picked up if

Records will be picked up, if union

Records will be picked up, for union

Records will be picked up, for because their column name is same in both tables.
The NATURAL JOIN keyword specifies that the attributes whose values will be matched between the two tables are those with matching names.

Records will be picked up, without any codition, Means its a INNER JOIN without comparision check.

A full outer join combines the results of both left and right outer joins

An INNER JOIN of own table


 Cursors are supported inside stored procedures and functions and triggers. The syntax is as in embedded SQL. Cursors in MySQL have these properties:

    * Asensitive: The server may or may not make a copy of its result table
    * Read only: Not updatable
    * Non-scrollable: Can be traversed only in one direction and cannot skip rows


7. How to quantize data in Database
database are quantified , by the size of the directory that
hold the database , and the number of entry on each tables

8. What is a “constraint”?
A constraint allows you to apply simple referential integrity checks to a table.

There are four primary types of constraints that are currently supported by SQL Server:

PRIMARY/UNIQUE - enforces uniqueness of a particular table column.
DEFAULT - specifies a default value for a column in case an insert operation does not provide one.
FOREIGN KEY - validates that every value in a column exists in a column of another table.
CHECK - checks that every value stored in a column is in some specified list
Each type of constraint performs a specific type of action.

9. What action do you have to perform before retrieving data from the next result set of a stored procedure?
Move the cursor down one row from its current position. A ResultSet cursor is initially positioned before the first row.

10. What are two methods of retrieving SQL?

11. What does the term unnormalized relation refer to? How did the normal forms develop historically from first normal form up to Boyc-Codd normal form?
Unnormalized relation will contain update,insert,deletion anamolies. 1nf will remove repeating colums. 2nf will remove subsets of data. In 2nf every non-key attribute should depend on entire primary key
In 3nf, all columns should depend directly upon primary key and transitive dependency should be removed(a->b b->c c->a).
In 4nf, multivalued dependancy should be removed.
In BCNF, every determinant should be a candidate key.