Interview Questions 1 - 10  «Prev Next»

Pojo Persistence Mechanisms(Questions)

  1. (J2SE) What are POJO persistence mechanisms?

    Answer:
    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. In Java SE 17, what is the difference between an Interface and an abstract class?

    Answer:
    In Java SE 17, both interfaces and abstract classes are used to achieve abstraction, a core principle of object-oriented programming. However, they serve different purposes and have distinct characteristics that influence their use in application design.
    Abstract Classes
    • Definition: An abstract class is a class that cannot be instantiated on its own and may contain both abstract and concrete methods. Abstract methods within an abstract class are declared without an implementation, and it is the responsibility of the subclass to provide concrete implementations for these methods.
    • Method Implementation: Abstract classes allow for both abstract methods (without an implementation) and concrete methods (with an implementation). This enables an abstract class to provide a partial implementation of its functionality.
    • State Holding: Abstract classes can hold state (fields) and can have constructors, allowing for more comprehensive encapsulation of behavior and state.
    • Inheritance: A class can extend only one abstract class due to Java's single inheritance model, which can be limiting if a class needs to inherit behavior from multiple sources.
    • Use Case: Abstract classes are suitable when there is a strong relationship between the abstract class and its subclasses, and when shared state or methods with implementations are needed.

    Interfaces
    • Definition: An interface in Java is a reference type that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields. The classes that implement interfaces must provide implementations for the interface's abstract methods.
    • Method Implementation: Prior to Java 8, interfaces could contain only abstract method declarations. Since Java 8, interfaces can also contain default methods with an implementation, and static methods with a body. This allows for a certain degree of implementation sharing.
    • State Holding: Interfaces cannot hold state. They can declare constants (public static final fields), but they cannot have instance fields.
    • Multiple Implementations: A class can implement multiple interfaces, overcoming the single inheritance constraint of abstract classes. This allows for more flexible design and the combination of disparate functionalities.
    • Use Case: Interfaces are ideal for defining a contract that various classes can implement, allowing for a decoupled system where the implementation can vary independently from the interface.

    Key Differences
    • Implementation Flexibility: Interfaces provide more flexibility through multiple implementations, whereas abstract classes are more restrictive due to single inheritance.
    • State and Constructors: Abstract classes can have instance variables and constructors, while interfaces cannot.
    • Method Types: Abstract classes can have a mix of method types (abstract, static, final), whereas interfaces have abstract methods, and since Java 8, can also have static and default methods, but all are implicitly public.
    • Design Intent: Abstract classes are used when subclasses share a common behavior that can be defined or partially implemented by the abstract class. Interfaces are used to define a contract that classes can implement, promoting a clear separation between the contract and its various implementations.

    Choosing between an interface and an abstract class in Java SE 17 depends on the specific design needs, considering factors like the relationship among the classes, the need for multiple inheritance, state encapsulation, and the extent of implementation sharing.
    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.

    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 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?

    Answer:
    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?

    Answer:
    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?

    Answer:
    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?

    Answer:
    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?

    Answer:
    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?

    Answer:
    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.
    TreeMap
    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?

    Answer:
    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.


SEMrush Software