Interview Questions 1 - 10  «Prev Next»

Pojo Persistence Mechanisms(Questions)

  1. (J2SE) What are POJO persistence mechanisms?

    A plain old Java Object, such as the Event object, supports primitive type properties, such as int, char, and Boolean, as well as objects, such as String and Date.

  2. (J2SE) What is the difference between an Interface and an abstract class?

    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 and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has one or more abstract methods.

  3. (J2SE) Describe synchronization with respect to multithreading.

    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 the same shared variable. This usually leads to significant run time errors.
    (Meaning you are not getting the result you expected) Synchronization is the ability to control the access of multiple threads to shared resources.

  4. What is protected access in Java?

    The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

  5. What is the difference between upcasting and downcasting?

    Explain upcasting and downcasting in Java.
    1. Upcasting: Java permits an object of a sub class to be referred by its super class. It is done automatically.
    2. Downcasting: It is to be performed manually by the developers. Here explicit casting is to be done by the super class.
    For example:
    Shape shape = new Shape() ; 
    Circle circ; 
    circ = (Circle) shape;

    where shape is a super class object. Now the methods of Shape can be invoked by the reference variable circ.

  6. What is the purpose of hashCode() in Java?

    Returns a hash code value for the object. Objects in Java have hashcodes associated with them.
    Object's hashcode is a signed number that identifies the object, but it is not unique for every object.

  7. What is the difference between Vector and ArrayList?

    Arraylists are fundamentally arrays that are growable and let you access the elements in the ArrayList through indices. Arraylist is a later addition to the Java Collections framework and was added with the version 1.2. The object 'Vector' has been present since the first version of the JDK. The List interface was later retrofitted into Vector. The main difference between the two is perhaps that vectors are synchronized while an Arraylist is not. For this reason, Arraylists are not thread-safe while vectors are. This will affect the performance and if your application does not need to be thread safe, then use Arraylist for more efficiency. Another difference is that Vector has a default size of 10 while Arraylist has no default size. In addition, Arraylist does not define an increment size. Vector is dynamic and its size can increase or decrease per the storage required by the program.

  8. How is the observer interface used in Java?

    There is a design pattern called Observer. It is useful to notify several objects (Observer) when something has happened to one instance (Observable).
    It is commonly used in Ajax to notify several objects when an event (like onclick) occurs.
    What you basically do is :
    1. Have an Observable class
    2. Have a few Observer classes
    3. Register the Observers to the Observable object (addObserver)
    4. Call notifyObserver when you want to notify all the observers that something has happened

  9. What is the difference between a HashMap and a TreeMap in Java?

    HashMap allows null as both keys and values. HashMap is useful when we need to access the map without considering how they are added to the map (unordered lookup of values using their keys).
    HashMap is synchronized while it is being looked up. HashMap does not allow duplicated entries. The performance of HashMap is based on two optional parameters which we can specify during the creation of the HashMap. 1) Initial capacity 2) load factor. Initial capacity is the bucket size assigned to a HashMap during it is creation.
    Load factor decides when the HashMap needs to be expanded. If the load factor is 0.75, the size will be increased when the current size of the map crosses 75% of its capacity.
    The basic difference between HashMap and TreeMap is that, in a TreeMap the elements are stored in a tree. TreeMap allows us to retrieve the elements in some sorted order defined by the user. We can say that TreeMap is slower than HashMap. This is the only implementation based on SortedMap interface.
    TreeMap allows us to specify an optional Comparator object during its creation. The keys should be compatible with the comparator specified. This comparator decides the order by which the keys need to be sorted.

  10. What does a TreeMap do if you do not specify a comparator?

    If we do not specify the comparator, TreeMap will try to sort the keys using natural order. This means the TreeMap will consider the keys as instances of the Comparable interface.