Q)Comparison Between collections and Arrays?


Collections :

1) Collections are not fixed In size.
2) With respect to memory collections are good.
3) With respect to performance collections shows worst performance.
4) Collections can hold heterogeneous data elements.
5) Every Collection class has built by using some Data structure.
6) Collection can hold only Objects but not primitives.

Arrays:

1.)Arrays are fixed In size.
2) with respect to memory arrays are not good.
3) With respect to performance the arrays are recommended to use.
4) Arrays can only hold homogeneous data elements.
5) There is no underlying Data Structure.
6) Arrays can hold both Objects and primitives.

List of JDBC Drivers?


1) Oracle Thin Driver

jdbcURL: jdbc:oracle:thin:@<HOST>:<PORT>:<SID>

DriverName :  oracle.jdbc.driver.OracleDriver

Eg :

Connection connection=null;

try{ Class.forName(DriverName);

connection=DriverManager.getConnection

(“jdbc:oracle:thin:@localhost:SID”,”user”,”pass”);

}catch(SQLException e){     e.printStackTrace();}

note:

     we can get SID from tnsnames.ora file and we can put IP address in the place of local host

eg: 122.122.122.122

2) JDBC ODBC Bridge Driver

jdbcURL:       jdbc:odbc:<DSN>

DriverName :  sun.jdbc.odbc.JdbcOdbcDriver

Eg :

Connection connection=null;

try{ Class.forName(DriverName);

connection=DriverManager.getConnection

(“jdbc:odbc:DSN”,”user”,”pass”);

}catch(SQLException e){     e.printStackTrace();}

note:  

     we can create DSN from Start->Settings->ControlPanel->AdministrativeTools->DataSource(ODBC)

3)Oracle OCI 8i Driver

jdbcURL:   jdbc:oracle:oci8:@<SID>

DriverName : oracle.jdbc.driver.OracleDriver

Eg :

Connection connection=null;

try{ Class.forName(DriverName);

connection=DriverManager.getConnection

(jdbcURL,”user”,”pass”);

}catch(SQLException e){     e.printStackTrace();}

note :

     we can get SID from tnsnames.ora files

4)Microsoft SQL Server

jdbcURL:  jdbc:weblogic:mssqlserver4:<DB>@<HOST>:<PORT>

DriverName: weblogic.jdbc.mssqlserver4.Driver

5)Microsoft SQL Server 2000(Microsoft Driver)

jdbc URL: jdbc:microsoft:sqlserver://<HOST>:<PORT>[;DatabaseName=<DB>]

DriverName: com.microsoft.jdbc.sqlserver.SQLServerDriver

6)IBM DB2

jdbcURL :  jdbc:db2://<HOST>:<PORT>/<DB>

DriverName: com.ibm.db2.jdbc.app.DB2Driver

7)MySQL

jdbcURL:  jdbc:mysql://<HOST>:<PORT>/<DB>

DriverName : org.gjt.mm.mysql.Driver

 8. Sybase (jConnect 5.2)

jdbc:sybase:Tds:<HOST>:<PORT>

com.sybase.jdbc2.jdbc.SybDriver

********************************************************************

     To test your driver once it’s installed, try the following code:

 {

  Class.forName(“Driver name”);

  Connection con = DriverManager.getConnenction(“jdbcurl”,”username”,”password”);

  //other manipulation using jdbc commands

}

catch(Exception e)

{

}

 

Java Interview Questions


1.How can you call the garbage collector.?

Ans). Garbage collector is automatically invoked when the program is being run. It can be also called by calling gc() method of Runtime class or System class in Java.

2. What is the difference between return and System.exit(0) ?

Ans). Return statement is used inside a method to come out of it. System.exit( 0) is used in any method to come of the program.

3. What is the difference between System.out.exit(0) and System.exit(1) ?

Ans). System.exit(0) terminates the program normally. Whereas System.exit(1) terminates the program because of some error encountered in the program.

4.) What is the difference between System.out ,System.err and System.in?

Ans). System.out and System.err both represent the monitor by default and hence can be used to send data or results to the monitor. But System.out is used to display normal messages and results whereas System.err is used to display error messages and System.in represents InputStream object, which by default represents standard input device, ie, keyboard.

5.)What is the difference between print( ) and println( ) method ?

Ans). Both methods are used to display the results on the monitor . print( ) method displays the result and then retains the cursor in the same line, next to the end of the result. println( ) displays the result and then throws the cursor to the next line.

6) Is String a class or data type ?

Ans). String is a class in java.lang package. But in Java, all classes are also considered as data types. So we can take String as a data type also.

7) What is the difference between String and StringBuffer classes?

Ans). String class objects are immutable and hence their contents cannot be modified.

         StringBuffer class objects are mutable, so they can be modified. Moreover the methods that directly manipulate data of the object are not available in String class. Such methods are available in StringBuffer class.

8) Can you declare a class as ‘private’ ?

Ans). No, if we declare a class as private , then it is not available to java compiler and hence a compile time error occurs, But inner classes can be declared as private.

9)What happens if main( ) method is written without String args[ ] ?

Ans). The code compiles but JVM cannot run it, as it cannot see the main( ) method with String args[ ].

10). What is the load factor for a HashMap or Hashtable ?

Ans). 0.75.

 

Q) Difference Between final, finally, finalize?


final:

         It is the modifier applicable for classes methods and variables. For final classes we can’t create child classes i.e inheritance is not possible.

         final() methods can’t be override in child classes for final variables reassignments is not possible because they are constants.

finally:

          It is a block associated with try catch the main objective of finally block is to maintain cleanup code which should execute always.

 finalize:

       It is a method should be executed by the “Garbage Collector” just before destroying an object. The main objective of finalize method is to maintain cleanup code.

 Note:  

         when compared with finalize, finally is always recommended to maintain cleanup code because there is no guarantee for the exact behavior of “Garbage Collector” it is Virtual Machine Dependent.