Sunday 8 June 2014

Java fresher interview questions

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 into HashSet. 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 list as 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 than ArrayList 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.
why string is immutable
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 for sleep() 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 to Runnable 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 JVM automatically 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.
}
}