Thursday, June 17, 2010

Java Interview Questions I

Here are some java questions I have been asked in interviews recently:
  1. Explain what the following JVM args are used for:
    • -Xss
    • -Xmx
    • -Xms
    • -XX:PermSize
    • -XX:NewSize
    • -server vs -client
  2. What is the difference between the heap and the stack? Explain what is present on the heap and stack when the following method is called:
    public void f(int i){
      Integer a = i ;
      double d = i ;
    }
    
    Answer: The primitive int 'i' is boxed into an Integer object and placed on the heap. So you have a "new Integer(i)" object present on the heap, and variables a, d and i on the stack (as well as the method f). [Further Reading]
  3. What are the main collections found in the Collections API?
  4. What is the difference between a List and a Set?
  5. How does a HashMap work?
  6. Given the following classes: Collection, Set, Map, List, ArrayList, Vector, LinkedList, Stack, HashMap, TreeMap, HashSet, TreeSet state which ones are interfaces, concrete classes and those that can be cast to Collection.
  7. What happens when the following code is executed?
    List<String> list = new ArrayList<String>() ;
    list.add("foo");
    list.add("bar");
    list.add("baz");
    
    for(String s : list){
        list.remove(s);
    }
    
    Answer: A ConcurrentModificationException is thrown.
  8. Given the following class:
    public class Person {
    
      private String name;
    
      public Person(String name){
        this.name = name;
      }
    
      public boolean equals(Object o){
        return name.equalsIgnoreCase(((Person)o).name);
      }
    
      public int hashCode(){
        return 0;
      }
    
      public static void main(String[] args) {
        Set<Person> set = new HashSet<Person>();
        set.add(new Person("David"));
        set.add(new Person("John"));
        set.add(new Person("DAVID"));
        set.add(new Person("Fred"));
    
        Map<Person, String> map = new HashMap<Person, String>();
        map.put(new Person("David"),"David");
        map.put(new Person("John"),"John");
        map.put(new Person("DAVID"),"DAVID");
        map.put(new Person("Fred"),"Fred");
      }
    }
    
    i) What is the size of the set and map? What is the result of map.get(new Person("David"))?
    ii) Delete the equals method and repeat i)
    iii) Delete the hashCode method and repeat i)
    iv) Delete both the equals and hashCode methods and repeat i)

    Answers:
    i) 3, 3, DAVID
    ii) 4, 4, null
    iii) 4, 4, null
    iv) 4, 4, null

  9. Describe the different kinds of exceptions?
  10. What happens when a RuntimeException is thrown but not caught by anything?
  11. How are JNI Exceptions handled?
  12. What does the following code print?
    public static void main(String[] args) {
     try {
      try {
       throw new Exception();
      }
      catch (Exception e) {
       System.out.println("FooI");
       throw e;
      }
      finally {
       System.out.println("FinallyI");
      }
     }
     catch (Exception e) {
      System.out.println("FooIII");
     }
     finally {
      System.out.println("FinallyII");
     }
    }
    
    Answer: FooI FinallyI FooIII FinallyII
More Interview Posts

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.