image source on Google Search
                 
          Most of the people and specially interview facing people looking interview related Question. Now I am try to show interview question on collection related and Database question. Hey common I am also facing these following questions one of interview.

Q) What is the Difference between ArrayList and Vector in Java?

R)

Vector
ArrayList
1. Vector came along with the first version of java development kit (JDK)
1. java.util.ArrayList was introduced in java version1.2
2. All the methods of Vector is synchronized
2.The methods of ArrayList is not synchronized
3. Vector uses Array internally as data structure
3. ArrayList uses Array internally as data structure
4. Vector doubles the size of its array when its size is increased
4. ArrayList increases by half of its size when its size is increased
5. Vector’s methods are synchronized 
5.ArrayList’s methods are not synchronized.
                
Q) Vector or ArrayList? Which is better to use in java?

R) In general, executing a ‘synchronized’ method results in costlier performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. But, when there is a certain need for thread-safe operation Vector needs to be used

Q) Is there an alternate available in java for Vector?

R)
ArrayList can be synchronized using the java collections framework utility class and then ArrayList itself can be used in place of Vector.
When there is no need for synchronized operation and you still look for better performance ‘Array’ can be used instead of ArrayList. But the development is tedious, since it doesn’t provide user friendly methods.
When you use Vector or ArrayList, always initialize to the largest capacity that the java program will need. Since incrementing the size is a costlier operation.


Q) What is the difference between Collection and Collections?


Collection
Collections
1. Collection is the root interface 
in collection hierarchy

1. Collections is a class which 
extends Object class & it
 consists exclusively static 
methods

2. it allows duplicate & non-duplicate
 elements  which may be ordered
 or unordered.
 
2. Collections are used to store, 
retrieve, manipulate, and 
communicate aggregate data
 
3. A collection (sometimes called 
a container) is an object that groups 
multiple  elements into a single unit.
 
3. Collections are used to store, 
retrieve and manipulate data, 
and to transmit data from one 
method to another. Collections
 typically  represent data items 
that form a natural group, like 
an order (a collection of order
 lines), a mail folder (a collection
 of messages), or a telephone 
directory (a collection of name/
phone-number pairs).
 
4. Collection is an interface which can
 be implemented the List, Set, Sorted 
Set, and Queue.
 
4. Collections is an utility class 
which contain the utility methods 
such as Sorting etc.
 
5. Collection(Uppercase C)represents 
the interface which is extended b
three other interfaces(Set, List and
 ueue,Map doesn’t extend Collection
 interface).
 Guys, its extended not Implemented 
(hope you all know the difference
 between the two,interface can 
extend interface, remember?)
 
5. Collections(upper case C with 
at the end) this the java.util.
Collections class that holds a pile
 of static methods  for use with 
collections
 


Note: after reading above difference you are able to understand which one is used when right…. Here I am mentioned some of the point while I am writing this time my mind is cached these points you people great you may know many things this post only for fast recollecting its not a measurement to  know this much okay friends.

Q)What is the difference between Hashtable and Hashmap ?

Hashtable
Hashmap
1. Hashtable was part of the original
 java.util and  is a concreate  
implementation of a Dictionary class.

1. HashMap implements Map interface.

2. Hashtable is synchronised
 
2. HashMap is not synchronised
 
3. Hashtable does not allow "null" key
 
3. HashMap allows "null" Key.
 
4. In HashTable you can change 
the iteration
 
4. HashMap you can change the iteration 
but you will get a 
java.util.ConcurrentModificationException.
 
 
Note: HashMap allows 1 null key and multiple null values but why? What was 
the basic idea behind it making such feature by JAVA builder?
 
R) HashMap allows only one null key, basic idea behind it is that key can have unique value 
only if one null is assigned to a key next null will be duplicate. You can try the following code 
and mail me what are your opinion friends.
HashMap hm=new HashMap();
hm.put("1","11");
hm.put("2","22");
hm.put("3","33");
hm.put("4","242");
hm.put(null,null);
hm.put(null,null);
 
HashMap also allow more null values or not check once and mail me.

Hashtable:

Hashtable is a data structure that retains values of key-value pair. It doesn’t allow null for both the keys and the values. You will get a NullPointerException if you add null value. It is synchronized. So it comes with its cost. Only one thread can access HashTable at a particular time.


import java.util.Map;
import java.util.Hashtable;

public class TestClass {

    public static void main(String args[ ]) {
    Map<Integer,String> states = new Hashtable<Integer,String>();
    states.put(1, "Andhra Pradesh");
    states.put(2, "Karnatka");

    states.put(3, null);    //will throw NullPointerEcxeption at runtime

    System.out.println(states.get(1));
    System.out.println(states.get(2));
//  System.out.println(states.get(3));

    }
}

HashMap:

HashMap is like Hashtable but it also accepts key value pair. It allows null for both the keys and the values. Its performance better is better than HashTable, because it is unsynchronized.
 
import java.util.HashMap;
import java.util.Map;

public class TestClass {

    public static void main(String args[ ]) {
    Map<Integer,String> states = new HashMap<Integer,String>();
    states.put(1, "Andhra Pradesh");
    states.put(2, "Karnatka");

    states.put(3, null);    // Okay
    states.put(null,"UK");

    System.out.println(states.get(1));
    System.out.println(states.get(2));
    System.out.println(states.get(3));

    }
}
  
Q) What is the difference between comparator and comparable?

R)

Comparator
Comparable
1.Comparator in Java is defined in java.util package

 
1. Comparable interface in Java is definedin java.lang package
 
2. Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
 
2. Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
 
3. Comparator in Java compare two objects provided to him
 
3. Comparable interface compares "this" reference with the object specified.
 
4.Comparator in Java is used to implement custom ordering of object.
public int compare(Object o1, Object o2)
4. Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implements Comparable interface.Its always good practice to override compareTo() for value objects.
 

Example Program:

1. public class Customer implements Comparable {
    private int customer_id;
    private String name;
    
    /**
     * Compare current customer with specified person
     * return zero if customer _id for both person is same 
     * return negative if current customer _id is less than specified one
     * return positive if specified customer _id is greater than specified one
     */
    @Override 
    public int compareTo(Object o) {
        Customer c = (Customer) o; 
        return this. customer _id - o. customer _id ;
    }
    ….
}

2. /**
 * Comparator implementation which sorts Customer objects on Customer _id field
 */
public class SortByCustomer _ID implements Comparator{

    public int compare(Object o1, Object o2) {
        Customer c1 = (Customer) o;
       Customer c2 = (Customer) o; 
        return p1.getCustomerId() - p2.get CustomerId();
    }
}

 Most frequently asking Java Interview question in telephonic or face to face code writing ?


Q) What is the difference between Observer and Observable in Java?


 R)

Observer is an object that wishes to be notified when the state of other object changes.(Listener,registered with the source to be notified of any change in the source) 


Observable is an object whose state may be of interest and in which other objects may register an interest(Source of an event/action)


import java.util.Observable;
import java.util.Observer;

/**
 * A simple demo of Observable->Observer
 */
public class ObservDemo extends Object {
  MyView view;

  MyModel model;

  public ObservDemo() {

    view = new MyView();

    model = new MyModel();
    model.addObserver(view);

  }

  public static void main(String[] av) {
    ObservDemo me = new ObservDemo();
    me.demo();
  }

  public void demo() {
    model.changeSomething();
  }

  /** The Observer normally maintains a view on the data */
  class MyView implements Observer {
    /** For now, we just print the fact that we got notified. */
    public void update(Observable obs, Object x) {
      System.out.println("update(" + obs + "," + x + ");");
    }
  }

  /** The Observable normally maintains the data */
  class MyModel extends Observable {
    public void changeSomething() {
      // Notify observers of change
      setChanged();
      notifyObservers();
    }
  }
}

Post a Comment

 
Top