Java Questions 51 - 100  «Prev Next»

Java Reference Types (Questions)

  1. What is the difference between an Interface and an Abstract class?

    Answer:
    An abstract class declaration has at least one instance method that is declared abstract, and this method will be implemented by the subclasses. An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior.

  2. What is the purpose of garbage collection in Java, and when is it used?

    Answer:
    The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it can no longer be referenced by the program in which it is used.

  3. Describe synchronization with respect to multithreading.

    Answer:
    With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.

  4. Explain the different ways of using a thread?

    Answer:
    A thread can be implemented by using the runnable interface or by extending the Thread class. The former is more advantageous, because when you are trying to achieve multiple inheritance this must be achieved using interfaces.

  5. What is the difference between pass by reference and passby value?

    Answer:
    Pass By Reference means passing the address itself rather than passing the value. “Passby Value” means passing a copy of the value to be passed. In Java, primitives are passed by value and objects are passed by reference.

  6. What is the difference between a HashMap and a Map?

    Answer:
    A Map is an interface and a Hashmap is a class that implements the Map interface.

  7. What is the difference between a HashMap and HashTable?

    Answer:
    The HashMap class is roughly equivalent to Hashtable except that it is unsynchronized and allows null values. (The HashMap allows null values as key-value pairs whereas the Hashtable does not). The HashMap does not guarantee that the order of the map will remain constant over time. The Hashmap is not synchronized and the Hashtable is synchronized.

  8. What is the difference between Vector and ArrayList?

    Answer:
    Vector is synchronized whereas arraylist is not.

  9. What is the difference between Swing and Awt?

    Answer:
    The AWT represents heavy-weight components and Swing represents light-weight components. Hence Swing works faster than AWT.

  10. What is the difference between a constructor and a method?

    Answer:
    A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class that has its own name, a return type (which may be void), and is invoked using the dot operator.

  11. What is an Iterator?

    Answer:
    Some of the collection classes provide traversals of their contents via a java.util.Iterator interface. This interface allows you to walk a collection of objects, operating on each object in turn. Iterators contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.