Java Questions 11 - 20  «Prev  Next»

Java Garbage Collection and the finalize() method

Garbage Collection and the finalize method
  1. What are 2 concepts regarding the finalize() that you need to remember?

    Answer:
    For any given object, finalize() will be called only once (at most) by the garbage collector.
    Calling finalize() can actually result in saving an object from deletion.

  2. What type of data structure do Maps represent?

    Answer:
    A Map is an interface.
    A Map cares about unique identifiers and you map a unique key (the ID) to a specific value, where both the key and the value are objects.

  3. What do the methods 1) equals 2) hashCode(), and 3) toString have in common?

    Answer:
    They are all public methods from the Object class.
    java.lang
    Class Object
    java.lang.Object
    

  4. In Java SE 17, What are the characteristics of the LinkedList interface?

    Answer:
    In Java SE 17, the `LinkedList` class is an implementation of the `List` and `Deque` interfaces, providing a doubly-linked list data structure. Here are the key characteristics of the `LinkedList`:
    1. Dynamic Data Structure: `LinkedList` dynamically allocates memory for elements, allowing for efficient insertion and removal operations. It does not require a contiguous memory block or pre-allocation of space.
    2. Doubly-Linked List: Each element in a `LinkedList` (known as a node) contains two references: one to the next node in the list and another to the previous node, enabling bidirectional traversal.
    3. Implements Interfaces: It implements `List`, `Deque`, and `Queue` interfaces, allowing it to be used as a list, double-ended queue, or queue.
    4. Element Access: Direct access to elements is not as efficient as in an `ArrayList` because it requires sequential traversal from the beginning or end of the list (whichever is closer) to reach the desired element.
    5. Insertion and Deletion: `LinkedList` allows for constant-time insertions and deletions at both ends of the list and anywhere within the list if an iterator is already at the position, making these operations faster compared to an `ArrayList`.
    6. Null Elements: `LinkedList` permits the inclusion of null elements.
    7. No Size Limitations: The list grows automatically as elements are added. There is no need to specify an initial capacity.
    8. Sequential Access Performance: Iterative operations perform well due to the sequential access nature of linked lists. However, random access is costly due to the need to traverse the list.
    9. Stack and Queue Operations: As a `Deque` implementation, it provides methods to support stack operations (`push`, `pop`, `peek`) and queue operations (`offer`, `poll`, `peek`).
    10. Not Synchronized: `LinkedList` is not synchronized, meaning that if multiple threads modify a `LinkedList` concurrently, it must be synchronized externally.
    11. Cloneable and Serializable: `LinkedList` implements the `Cloneable` and `Serializable` interfaces, so it can be cloned and serialized.
    12. Memory Overhead: Each element in a `LinkedList` has more memory overhead compared to `ArrayList` because of the additional previous and next pointers.

    In summary, `LinkedList` is suitable for applications where frequent insertions and deletions are required, and where memory allocation is more flexible. However, if fast random access to elements is a priority, an `ArrayList` might be preferred over a `LinkedList`.
    A linkedlist is orderd by index position, like ArrayList, except that the elements are doubly-linked to one another.
    Basic Rule: If you need to remove items from the beginning or the middle of a List, then use a LinkedList. For a normal sized list (one that is not too large), it does not matter what kind of list you use and by default you are going to use ArrayList.


  5. What are the 4 reference types in Java?

    Answer:
    1. Array: In Java, all arrays are heap objects.
    2. Enum
    3. Classes
    4. Interfaces

  6. How do you create Java constants?

    Answer:
    Java constants are created by marking variables 1) static and 2) final.

  7. What are the properties of a Javabean?

    Answer:
    Think of properties as private instance variables.

  8. What is the purpose of events in Javabeans?

    Answer:
    Events allow components to notify each other when something happens.

  9. When are Javabeans mentioned explicitly on the exam?

    Answer:
    If the exam question is asking about naming conventions, not just whether an identifier will compile, Javabeans will be mentioned explicitly.

  10. How many public classes can there be per source code file?

    Answer:
    One.

SEMrush Software