Java Questions 51 - 100  «Prev Next»

Array, ArrayList (Interview Questions)

  1. How can you compare arrays for equality?

    Answer:
    You can compare arrays for equality with the convenience method Java.util.Arrays.equals()

  2. What is the purpose of cloning in object-oriented programming?

    Answer:
    In object-oriented programming (OOP), cloning is the process of creating an exact copy of an object, including its state and behavior. The purpose of cloning is to:
    1. Create a duplicate object: Cloning allows you to create a new object with the same attributes and values as the original object, without modifying the original object.
    2. Preserve object state: Cloning helps preserve the state of an object, including its internal data and settings, so that the duplicate object can be used independently.
    3. Improve performance: Cloning can be more efficient than creating a new object from scratch, especially for complex objects with many attributes and dependencies.
    4. Support object modification: Cloning allows you to modify the duplicate object without affecting the original object, which is useful in scenarios where you want to experiment with different versions of an object.
    5. Enable object sharing: Cloning enables sharing of objects between different parts of a program, while ensuring that each part works with its own independent copy of the object.
    6. Facilitate object serialization: Cloning is useful when serializing objects, as it allows you to create a copy of the object that can be written to a file or transmitted over a network.
    7. Support object-oriented design patterns: Cloning is used in various design patterns, such as the Prototype pattern, which relies on cloning to create new objects based on existing ones.
    Overall, cloning is a powerful feature in OOP that enables flexible and efficient object creation, modification, and sharing.


  3. What must you do to create a "deep copy" in Java?

    Answer:
    To make a deep copy you must create cloned copies of all components (excluding Strings and primitive values) by invoking their respective clone methods.

  4. What type of Exception class is CloneNotSupportedException?

    Answer:
    It is a checked exception class.

  5. What are two different ways to view an array?

    Answer:
    1. You can view it as a collection of individual indexed variables like
    temp[0],..., temp[n]
    
    2. You can view an array as one large composite object that has a number of different values all of the same type



  6. In Java, what is the difference between String and StringBuffer?

    Answer:
    The key differences between String and StringBuffer in Java:
    1. Mutability:
      • String: Immutable. Once a String object is created, its contents cannot be changed. Any operation that seems to modify a String actually creates a brand new String object.
      • StringBuffer: Mutable. Designed for modifications. You can append, insert, delete, or reverse characters within a StringBuffer object without creating a new object each time.
    2. Performance:
      • String: Faster for simple operations where you don't need to change the string frequently. Immutability makes working with Strings in predictable ways easier.
      • StringBuffer: Generally faster when you have a lot of string manipulations (concatenation, appending, etc.) occurring.
    3. Thread Safety:
      • String: Thread-safe. Since Strings are immutable, multiple threads can safely access a String object without worrying about synchronization.
      • StringBuffer: Also thread-safe. Its methods are synchronized, meaning only one thread can modify a StringBuffer object at a time. This is important for multithreaded environments.
    4. Memory Management:
      • String: Less memory efficient in scenarios where there are many changes to the string. Because it's immutable, each change creates new objects leading to more memory use.
      • StringBuffer: More memory efficient for scenarios with frequent string modifications. It modifies the existing object rather than generating new ones.

    When to Use Which:
    • String: Use this when you know the string value won't change, or if you have basic, infrequent operations on the string. It's great for representing fixed textual data.
    • StringBuffer: Choose StringBuffer when the string needs to be modified frequently, especially in multithreaded applications where thread safety is crucial.

    Important Note:
    • Java also has a `StringBuilder` class. It's very similar to `StringBuffer` but is not thread-safe. Use `StringBuilder`instead of `StringBuffer`if you don't need the thread safety and want a slight boost in performance.
    • The String Buffer class represents a mutable string of characters that can grow or shrink as necessary. Its mutability makes it suitable for processing text in place which is not possible with the immutable String class.


  7. What does "protected" visibility for 1) fields and 2) methods allow?

    Answer:
    Because it is fairly common for a subclass method to reference data fields in its superclass, Java provides a less restrictive form of visibility called protected visibility. A data field (or method) with protected visibility can be accessed in either the class defining it, in any subclass of that class, or any class in the same package.

  8. When would you advise to use an ArrayList?

    Answer:
    ArrayList objects are used most often when a programmer wants to be able to add new elements to the end of a list but still needs the capability to access the elements stored in the list in arbitrary order. The ArrayList class implements a growable array of objects.

  9. What is IllegalMonitorStateException?

    Answer:
    This signals an illegal monitor state. It is thrown by the Object notify() and wait() methods used for thread synchronization.

  10. What is the advantage of "ArrayList" over Vector?

    Answer: ArrayList is like Vector without the synchronization overhead.
    Vector and Stack are legacy implementations left over from Java 1.0