AddThis Social Bookmark Button

In this section we will have a list of questions , my personal collection, which I encountered or derived from my understanding of java and this is highly recommended for experienced Java developers.

General Collections

    1. What is a load factor ?
    2. Which of the collection class would you user if you have to implement a queque behavior ?
    3. What is size and capacity of a Vector ?
    4. Which situation would lead a Vector to throw an ArrayIndexOutOfBoundsException ?
    5. Can you iterate a map ? 
      No, Map does not implement Iterable interface, only Collection ( thus , list and set ) do , so the Collection implements iterable <E> , make the set and the list iteratble. The Map can't be iterable only the key part can be iterable for example the following code is how you traverse the map 

Map <String,String> map= new HashMap<String,String>();

Ierator i =map.keySet().iterator();

while(i.hasNext()){

String value=map.get(i.next());

}

  1. How can you make your custom  class iterate through advanced for loop ?
    You need to make your class implement the Iterable interface, override the iterator() method , and create Iterator class , which can iterate your items in the class.
  2.  

HashCode,Equality , toString,identity

  1. Suppose that I am designing a class it is really mandatory to override the hashCode() method , even if I am sure none of these object will ever be used in Hash based collections like HashMap, Hashtable or HashSet ?
    It is not necessary to override the hashCode method even if you don't use it in a hashbased collections. But If you have your equals method overriden , which means that you would want your object to be tested for equality. And the contract with the equals method is that , if two objects are equal you must override the hashcode method. Lets take the following example
    class Animal {
    	private int i=12;
    }
    public class Test {
        public static void main(String [] args) throws Exception {
            Animal a1=new Animal();
            Animal a2=new Animal();
            System.out.println(a1==a2);
            System.out.println(a1.equals(a2));
        }
    }
    
    Looking at the code you certainly know that the a1 and a2 objects are equal meaningfully, But the output would say otherwise, ( false false), This is not expected. What the above equals does is , it invokes the equals method on Object , and the object checks if they are pointing to the same object, which is not the right way to check. So essentially you would have to override the equals method, and as per contract with hashCode, the whenever the equals is overriden it is necessary to override the hashCode(), who knows you might need this objects added to the hash based collections, so better do it now.
  2. When I invoke system.out.println() on an object which class doesn't override the toString() method , what is the output and what it means ?
    The output comes in a format of <fully qualified class name>@hashcode, the hashcode (in hexadecimal) is invoked from the Object if hashCode method is not overriden
  3. What is the problem if you return a random number in your hashCode(), I think it is most efficient , since it would create unique bukkets ?
  4. How good idea is that to return 1 ( a contstant value ) in your hashCode()?
    In such case all your object will fall into one single bucket , and the searching algorithm will end up invoking the equals method to find the object you are looking for , which is a highly inefficient way , but legal.

Seriallization , Deseriallization, Externalization

  1. Lets say you need to introduce a marker interface in your library, how would be add capability to the class which implements it?
  2. Static fields don't get seriallized, but lets say I would want to save this the state of the field , how would I do it ?

Generics

  1. If  methods overloaded on basic of type parameters, what is the best alternative to overload ?


 Java Memory Management

  1. How do you set the stack size of java runtime ? 

    -Xss can be used to set the stack size

  2.  What is the difference between stackoverflowerror vs outofmemoryerror ? 

     stackoverflow happens in overflow of stack ,and outofmemory ocuures if you dreained your heap.

  3. How to generate StackOverlFlow error and OutOfMemoryError problematically ?