Monday, 22 July 2013

15 Most important Java interview questions for fresher?

Q1. What is the difference between HashMap and Hashtable class.
Hashtable
HashMap
Hashtable class is synchronize.
HastMap is not synchronize.
Because of Thread-safe, Hashtable is slower than HashMap
HashMap works faster.
Neither key nor values can be null
Both key and values can be null
Order of table remain constant over time.
does not guarantee that order of map remain constant over time.

Q2. How to remove duplicate element from a ArrayList.
At first this question will look quite difficult but the answer is very simple. By converting the ArrayList intoHashSet. When you convert a ArrayList to HashSet all duplicates elements will be removed but insertion order of the element will be lost.

Q3. How to create a unmodifiable/read-only list in Java?
Collections.unModifiableList() method is used to create unmodifiable/read-only list in Java. Pass the listas a argument to this method.

ArrayList al=new ArrayList();
al.add("a");
al.add("b");
al.add("c");
al = Collections.unModifiableList(al);

Q4. What is the difference between ArrayList and Vector class.
Vector
ArrayList
Vector class is synchronize.
ArrayList is not synchronize.
Because of Thread-safe, Vector is slower thanArrayList
ArrayList works faster.
Enumeration is used to iterate through the element of Vector.
Iterator is used to iterate through the element of ArrayList.


Q5. Why String is immutable in Java?
In Java, strings are object. There are many different ways to create a String object. One of them is using string literals.

Each time a string literal is created, JVM checks the string constant pool first. If string already exist in the pool, a reference to the pool instance is returned. Sometime it is possible that one string literals is referenced by many reference variable. So if any of them change the string, other will also get affected. This is the prominent reason why string object is immutable.

Q6. Difference between StringBuffer and StringBuilder?
Both StringBuffer and StringBuilder classes are almost same except for two major differences.
StringBuffer
StringBuilder
StringBuffer is synchronize.
StringBuoder is not synchronize.
Because of synchronisation, StringBuffer operation is slower than StringBuilder.
StringBuilder operates faster.


Q7. Difference between equals() method and == operator?
Main difference between '==' and equals() in Java is that '==' is a operator used to check whether two different reference refers to same instance and equals() is a method used to check equality of an object. Another important difference is '==' operators is used more with primitive data type while equals() method is used for object.
   See example of '==' operator and equals() method for more detail.

Q8. What are the different ways to create a thread in java?
There are two different way to create a thread in java.
1. Extending Thread class.
2. By implementing Runnable interface.

See  how thread are created in java  for more detail.

Q9. What are the difference between sleep() and wait() method.
wait() method in java should be called from synchronized block while there is no such requirement forsleep() method. Another difference is sleep() method is a static method, while wait() is an instance specific method called on thread object. In case of sleep(), sleeping thread immediately goes toRunnable state after waking up while in case of wait(), waiting thread first acquires the lock and then goes into Runnable state. notify() and notifyAll of Object class are used to awake a waiting Thread while sleeping thread can not be awaken by calling notify() method. wait() method is defined in Object class while sleep() is defined in Thread class.

Q10. What is serialization in java?
Serialization is a process of converting object into a sequences of byte which can be written to disk or database or sent over network to any other running JVM. The reverse process of creating object from sequences of byte is called Deserialization.
See more detail on Serialization.

Q11. What is covariant return type?
After Java 5, it is possible to override a method by changing its return type. If subclass override any method by changing the return type of superclass method, then the return type of overriden method must be subtype of return type declare in original method in the superclass. This is the only way in which method can be overriden by changing its return type.

Q12. How to find if JVM is 32 or 64 bit from Java program.
By using System.getProperty() method, you can find JVM size from java program. Although the size of JVM hardly matters because we know that java is platform independent i.e write once and run everywhere but in some situation it is required to know the size of JVM like if you are using native library. Example of how to get size of JVM.
System.out.println(System.getProperty("os.arch"));
System.out.println(System.getProperty("sun.arch.data.model"));

Q13. How to change file permission in Java?
setReadable()setWritable()setExecutable() method is used to change or set file permission in java.
See more file example.

Q14. How you can force the garbage collection?
Garbage collection automatic process and can't be forced. You could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately. Garbage collection is one of the most important feature of Java, Garbage collection is also called automatic memory management as JVMautomatically removes the unused variables/objects (value is null) from the memory. User program can't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. I Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.

Q15. What do you understand by Synchronization?

Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. Synchronization prevents such type of data corruption.
E.g. Synchronizing a function:
public synchronized void Method1 () {
// Appropriate method-related code.
}
E.g. Synchronizing a block of code inside a function:
public myFunction (){
synchronized (this) {
// Synchronized code here.
}
}

Saturday, 20 July 2013

What is joins and how it is useful in SQL?


Joins:
              When data is required from more than one table or view then one table or view should be join with other table or view in the means of columns. Then the columns in different tables or views should match in their data types and data. Columns names are may or may not be equal.
                     The joins are performed when FROM clause contains more than one table or view in their SQL query. If FROM clause contains N tables then the minimum requirement is N-1 JOINS
IMPORTANT RULES ABOUT JOINS:
·        The SELECT clause columns are may  or may not present in JOIN statement.
·        If any columns are contains LOB data types then that columns are should not be used in WHERE clause statement.
·        The optimizer determines the order in which join the table based on “given join conditions and Indexes upon the tables”.
·        If more than one table or   view contains the same column name then optimizer may confused which table column you want. Use “.” Operator for representing the column name with table name like TABLE NAME.COLUMN NAME.
·        In normal conditions also if you are using column name in select statement with the help of table name then data access is enhanced   compared to normal   one.
Ex:
         Consider two table like employee (employee no, employee name, manager, salary ,hire date, commission ,department no)  and department (department no, department name, location) . We want a result which displays the employee number, salary, department no, department name, location.
                   Then we must combine these both tables and produce a  result without a duplicate record.
SQL >
SELECT employee number, salary, department no, department name, location FROM employee, department;
               This query display the result in Cartesian product. That is if employee and department tables contains 14 and 4 records then result will be 14 X 4 = 56 records.
             This can be eliminated by using the JOINS
SQL >
SELECT employee number, salary, department no, department name, location FROM employee E, department D WHERE E. department no= D . department no;
NOTE: The columns should match in their data types and data. Columns names may or may not be equal.
There are different types of Joins are present in SQL.
EQUI JOIN:
                      The join is based upon the data type and data present in the columns those are used in join statement. The columns names are may or may not be equal. The EQUI join is done by using the equal operator “=”.
SYNTAX:
                  SELECT col1, col2,………..
                        FROM table1, table2
                        WHERE table1.col=table2.col
EX:
1.     SQL >
SELECT employee number, salary, department no, department name, location FROM employee E, department D WHERE E. department no= D . department no;
When you are using the same data type and data but one column data is different from other column data of different tables then join performed but result will not be displayed.
EX:
 SQL>
             SELECT employee name, employee no, department name, location
             FROM     employee    E, department D
             WHERE   E. employee name= D. department name;
                 In the above example the data type and data are similar but data is not relate to one to other. In this situation EQUI join performs but “no records are selected” feedback is given by the server.
NON EQUI JOIN:
                               The Non Equi join combines two tables data in usage of relational operator other than “=”. The NON EUQI performs only when one table column data is falls in the range of other table columns data. It uses the between operator so it is also called   “BETWEEN JOIN”.
SYNTAX:
                  SELECT col1, col2,………..
                        FROM table1, table2
                        WHERE table1.col BETWEEN table2.col1 AND table2.col2
Ex:
         Consider Salary grade is another table it contains SALARY GRADE(grade, low salary, high salary).
   SQL>   
                    SELECT employee name, employee no, salary, grade
                    FROM employee E, salary grade s
                     WHERE     E. salary between s. low salary and s. high salary;
NOTE:
 Here “AND” is not logical operator .it is simply operator with   combination of “BETWEEN”

                    


Friday, 19 July 2013

what is the main difference in between PRIMARY KEY and NOT NULL +UNIQUE KEY ?

Primary key:

                                      The key which is useful for identify a record  uniquely in a table.The primary key is a combination of both not null and unique key constraints.the primary key is also useful in references.Primary key creates two data base objects that is one for key and one for automatic index.


Not Null:

                  Which does not allows a null values into a table.Not Null creates only one object in data base.




Unique key:

                                        The key which does not allows a duplicate values into column is Unique key.the name it self denotes , one that does not matches with other.As like primary key the unique key is also contains two data base objects one for key and other for automatic index.


          PRIMARY KEY
UNIQUE + NOT NULL
1.     One table can have only one   primary key.

2.     Two data base objects are created

3.     We can’t drop either unique or not null behavior separately.


4.     It do the best performance in case of composite primary key
1.     One table can have more than one unique + not null key.

2.     Three data base objects are created. That is one for NOT NULL and two for UNIQUE KEY.

3.     We can drop separately.




4.     Low performance.(more not null constraints are required with unique key)

Tuesday, 16 July 2013

In how many ways we can create a instance to a class in JAVA?

We can create a instance in java in "Four" different ways. They are



1. Using new keyword
 we can create the instance normally using new operator which is called as static instance creation.
        Hello hello =  new Hello();

2.using Class.forName()
we can create the instance dynamically without using new operator as follow
Hello hello=(Hello)Class.forName("com.bikash.Hello").newInstance();
                                   or
 Class cls = Class.forName("com.bikash.Hello");
Hello hello = (Hello)cls.newInstance();

3.using clone().
clone() method can be used to copy of the existing object.
Hello hello=new Hello();
Hello hello1=(Hello)hello.clone();



4.using object deserialization.
deserializion is the process of creating the new object on the remote mechine from its serialize form.
ObjectInputStream ois =new ObjectInputStream();
Hello hello = (Hello)ois.readObject();

Thursday, 11 July 2013

What is the difference between delete and truncate command in ORACLE or SQL?



Truncate Command:


  • Truncate command is used to delete the all records in a table.The table skeleton won't changed.
           Syntax  : Truncate table <table name>
  • Roll back is not possible in Truncate command.
  • After execution of truncate command explicit commit is called
  • Condition Based filtering of records is not possible in Truncate command. 
  • It is DDL command,Explicit commit calls by itself.
  • It is very fast compared to the "Delete" command

Delete Command: 

  • Delete command is used to delete the particular record form a table. 
         Syntax : Delete from <table name> where condition
  • Roll back is possible in Delete Command.Because it stores the record in roll back memory.
  • It is DML command so it requires the explicit commit statement by the programmer.
  • It requires the condition to remove the data from a table.
  • It removes the one record at a time.
  • It is slow compared to "Truncate" Command.

     

Tuesday, 9 July 2013

Polymorphism Concept in OOP languages

polymorphism :
                                polymorphism is a concept which describes about the different forms of a
particular method.for example take "water" is a method in a class of  "liquids".the method water may have different forms like "solid form(ice)" or may "fuel form(liquid)".that is poly means many and morphism means different forms.
                             in object oriented languages polymorphism is divided in to two categories
                                             1.Compile Time
                                              2.Run Time
1.Compile time:
                            Compile time polymorphism is "Method Overloading".
2.Run Time:
                             Run Time polymorphism is "method Overriding".
OVERLOADING:
                       The datatypes of parameters or number of parameters of method is changes in one form to other then it is know as "Method Overloading".
                Ex:        1.public void add(int a,int b)
                             2.public void add(int a,int b,int c)
                                                                         In above two cases the method   is overloaded. by changing the number of parameters.
                    
                                  1.public void add(int a,int b)
                                   2.public void add(float a,float b).
                                                                        In above two cases the method is overloaded by using the changing of datatypes.
OVERRIDING:
                            The signature of a method wont change in subclass which is used in super class but the method body is override.
                                EX:    Class A{
                                                                public int add(int a,int b)
                                                                    {
                                                                         SOP("addition is"+(a+b));
                                                                      }
                                                            }
                                               Class  B extends A{
                                                                              public int add(int a,int b)
                                                                    {
                                                                         int c=10;
                                                                        SOP("addition is"+(a+b+c));
                                                                      }
                                                     In the above the method is override in sub class
Advantages:
                      we can define our own code in sub class by using this method overriding with out changing the signature of method of super class.