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 Java multithreading in Java SE 17.

    Answer:
    In Java SE 17, synchronization pertains to the mechanism that controls access to multiple threads to shared resources or critical sections to prevent data inconsistency and ensure thread safety. Java provides built-in support for thread synchronization through synchronized blocks and methods, as well as through higher-level constructs from the `java.util.concurrent` package.
    • Synchronized Methods A synchronized method ensures that only one thread can execute any synchronized method on the same object instance at a time. When a thread enters a synchronized method, it acquires the intrinsic lock (or monitor lock) of the object on which the method is invoked. If another thread attempts to invoke a synchronized method on the same object, it will be blocked until the first thread exits the synchronized method and releases the lock.
    • Synchronized Blocks Synchronized blocks in Java allow more fine-grained control over the synchronization. Instead of locking the entire method, a synchronized block can specify a particular object that the thread must lock on. This approach provides more flexibility and can reduce contention by not blocking access to other synchronized methods or blocks that do not require the same lock.
      synchronized (object) {
          // Critical section code here
      }
      
    • Locks and Condition Variables Beyond the basic `synchronized` keyword, Java SE 17 offers a rich set of concurrency utilities in the `java.util.concurrent` package, including explicit lock objects (`Lock`), which provide more extensive lock operations than intrinsic locks. Unlike synchronized methods and blocks that are implicitly managed by the JVM, explicit locks offer advanced features like try-lock, timed lock acquisition, and interruptible lock waiting. `ReentrantLock` is a commonly used implementation of `Lock` that allows the lock to be acquired multiple times by the same thread without causing a deadlock.
      Condition variables (`Condition`), used in conjunction with lock objects, allow threads to wait for specific conditions to become true within a locked region, providing a means for more complex thread communication and coordination.


    • Volatile Keyword: The `volatile` keyword in Java is a lighter synchronization mechanism compared to `synchronized` blocks and methods. A `volatile` variable guarantees visibility of changes to variables across threads. When a variable is declared `volatile`, the JVM ensures that any write to that variable is immediately visible to other threads, and that reads from that variable always fetch the latest written value. However, `volatile` does not provide atomicity; it's suitable for cases where only visibility concerns exist, not compound actions (like incrementing a value).
    • Atomic Variables: For scenarios requiring atomic operations on single variables without using `synchronized`, Java SE 17 offers atomic classes like `AtomicInteger`, `AtomicLong`, `AtomicReference`, etc., in the `java.util.concurrent.atomic` package. These classes provide methods for performing atomic operations, such as compare-and-set, increment, and get-and-update, ensuring thread safety without the overhead of synchronization.
    • Best Practices: Effective synchronization in Java requires careful consideration to balance between ensuring data integrity and minimizing thread contention, which can lead to performance bottlenecks. Overuse of synchronization can lead to thread contention and reduced parallelism, while insufficient synchronization can cause data inconsistency and subtle bugs. Developers are advised to aim for minimal locking granularity, prefer `java.util.concurrent` utilities for better scalability, and carefully assess the needs for synchronization in the context of their specific multithreading scenarios.

    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 1) pass by reference and 2) 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.

SEMrush Software