Daily Archives: October 23, 2013

Java Interview Questions – Error and Exception Handling

  • What is Exception chaining in Java?

One of the important exception handling concept in Java when a different exception is thrown in response to an actual exception. This in turn will create a chain of exceptions. This is commonly used to wrap a checked exception to unchecked exception under various scenarios and such coding prevails when actually writing a framework in which case a known checked exception is thrown as an unchecked framework exception. Even though new exception is thrown, as a best practise always include the root cause (exception) in the newly created exception class.

  • With regards to JDK7 there is a new feature with regards to Exception handling. Can you detail this?

There are numerous small and medium new features on JDK7 which are very useful. Some of them with regards to error and exception handling is as detailed below:-

    • Multiple exception handling in one catch block

Reduces code cluttering and are more readable.

    •  ARM (Automatic Resource Management) blocks

As the name suggests it is capable of automatically handling resources on behalf of you, also known popularly as try with resource. Takes lot of botheration away from developers. Below is the example from oracle java docs:

Page Visitors: 1620

Java Interview Questions – Collections

  • What is the key requirement for an object to be used as a key in hash based collection e.g. HashMap or Hashtable or ConcurrentHashMap?

The object should implement both equals() and hashcode() methods.

  • What are immutable classes in Java? How to make immutable class in Java?

Immutable classes are classes, whose objects once created cannot be modified once created. Any modification to these immutable objects will result in another immutable object. Example classes are String and StringBuffer classes in Java.

To make an immutable class the class should be made final. All the fields in the class should also be made final and utmost care should be taken so as to ascertain that object reference must not leak during construction phase of the object.

  • What happens internally when you invoke get(Key key) method in hash based collections like HashMap and Hashtable?

The following steps happen:

      • The Key.hashcode() method gets invoked to get the bucket location in the array backed Hash collections.
      • In the backing array the keys and values are stored in an internal class called as Entry. If there is only one Entry at the bucket location, the value is returned. If due to some reasons two keys have same hashcode (this happens), the bucket location will have two entries and it forms a sort of a linked list data structure. In that case, it traverses through the list comparing keys in each entry using keys.equals() until it return true. Once true is returned, the value is returned.
  • Why should an object which is to be used as key in collection classes need to be immutable?

This is required because the hashcode() method when being called should always return the same value.

  • Have you heard of ConcurrentHashMap, if so what is it?

This is an alternative to Hashtable in Java and as the name suggests, it is synchronized, thread safe and highly usable in multi-threaded programming.

  • How to make HashMap synchronized in nature?

You can use Collections.synchronizedMap(HashMap). This will return a collection very much similar to Hashtable.

  • What are the difference/similarities between HashSet and TreeSet?

Some of the difference are:-

    • HashSet is faster than TreeSet with regards to performance.
    • HashSet doesn’t preserve ordering but TreeSet does.
    • HashSet allows null objects but TreeSet doesn’t.
    • HashSet is backed by HashMap and TreeSet is backed by TreeMap.

Some of the similarities are:-

    • Both are thread-safe and synchronized in nature.
    • Both implement Set interface

Reference:-

Awesome post (Hats Off) – http://java67.blogspot.ae/2013/07/15-advanced-core-java-interview-questions-answers-senior-experienced-5-6-years-programmers-developers.html

Page Visitors: 1001