Java Common Interview Questions 1

  Collect and sort out some Java interview questions that are commonly used in projects and that are often asked in interviews.

  1. Basic:  

   1.   What does the static keyword mean? Is it possible to override a private or static method in Java? Is it possible to access non-static variables in a static environment?

 

      1. The static keyword indicates that a member variable or member method can be accessed without the instance variable of the class to which it belongs.

      2. Static methods in Java cannot be overridden. why? Because method overriding is based on runtime dynamic binding, while static methods are statically bound at compile time.

        A static method is not associated with any instance of the class, so it is not conceptually applicable.

      3. A static variable belongs to a class in Java, and its value is the same in all instances. When a class is loaded by the Java virtual machine, static variables are initialized.

        If your code tries to access non-static variables without an instance, the compiler will complain, because these variables have not been created and are not associated with any instance. 

  2.  What are the similarities and differences between interfaces and abstract classes?

      Same point: 

      1. Neither can be instantiated      

      2. Can contain abstract methods, which are used to describe the functions of the class, but cannot provide specific implementations      

      The difference is as follows:

      1. There can only be abstract methods in the interface, and non-abstract methods can be written in the abstract class, so as to avoid writing them repeatedly in the subclass, which can improve the reusability of the code.
      2. A class can only inherit one direct parent class, which can be a concrete class or an abstract class; however, a class can implement multiple interfaces.
      3. The variables declared in the Java interface are final by default. Abstract classes can contain non-final variables.
      4. Member functions in Java interfaces are public by default. Member functions of abstract classes can be private, protected or public
      5. When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces.
 
   3. What is pass-by-value and pass-by-reference?

    1. Objects are passed by value, which means that a copy of the object is passed. Therefore, even changing the object copy will not affect the value of the source object.

    2. Objects are passed by reference, which means that what is passed is not the actual object, but a reference to the object. Therefore, external changes to the referenced object will be reflected in all objects.

  Second, the Java collection:
  1.  What are the basic interfaces of the Java Collections Framework?
    
    1. Collection: Represents a group of objects, each of which is its child element.

    2. Set: A Collection that does not contain duplicate elements.

    3. List: an ordered collection and can contain repeating elements.

    4. Map: An object that can map keys to values, and keys cannot be repeated.

  2.  What is the difference between Iterator and ListIterator?


    1. Iterator can be used to traverse Set and List collections, but ListIterator can only be used to traverse List.

    2. Iterator can only traverse the collection forward, and ListIterator can be forward or backward.
    3. ListIterator implements the Iterator interface and includes other functions, such as: adding elements, replacing elements, getting the index of the previous and next elements, and so on.

  3.  What is the difference between HashMap and Hashtable?

    1. Both HashMap and Hashtable implement the Map interface, so many features are very similar.

    However, they have the following differences:

    2. HashMap allows keys and values ​​to be null, while Hashtable does not allow keys or values ​​to be null.

    3. Hashtable is synchronous, while HashMap is not. Therefore, HashMap is more suitable for single-threaded environment, while Hashtable is suitable for multi-threaded environment.

    4. HashMap provides a collection of keys for application iteration, so HashMap is fail-fast. On the other hand, Hashtable provides an enumeration of keys (Enumeration), and Java5 provides ConcurrentHashMap, which is an alternative to HashTable and has better scalability than HashTable.

  4. What is the difference between HashSet and TreeSet?

    1. HashSet is implemented by a hash table, so its elements are unordered. The time complexity of add(), remove(), contains() methods is O(1).

    2. TreeSet is implemented by a tree-like structure, and the elements in it are ordered. Therefore, the time complexity of add(), remove(), contains() methods is O(logn).

  5. What is the difference between an Array (Array) and a List (ArrayList)? When should I use Array instead of ArrayList?

    1. Array can contain basic types and object types, and ArrayList can only contain object types.

    2. The size of Array is fixed, and the size of ArrayList changes dynamically.

    3. ArrayList provides more methods and features, such as: addAll(), removeAll(), iterator() and so on.

    4. For basic types of data, collections use autoboxing to reduce coding effort. However, this approach is relatively slow when dealing with fixed-size primitives.

  6. What is the difference between ArrayList and LinkedList?

    Both ArrayList and LinkedList implement the List interface,

    They have the following differences:

    1. ArrayList is an index-based data interface, and its bottom layer is an array. It can perform random access to elements in O(1) time complexity.

    2. LinkedList stores its data in the form of a list of elements, each element is linked to its previous and next elements, in this case, the time complexity of finding an element is O(n) .

     3. Compared with ArrayList, LinkedList's insertion, addition, and deletion operations are faster, because when an element is added to any position in the collection, there is no need to recalculate the size or update the index like an array.

    4. LinkedList occupies more memory than ArrayList, because LinkedList stores two references for each node, one points to the previous element and one points to the next element.

  7. What are the best practices for the Java Collections Framework?

    1. It is very important for performance to correctly choose the type of collection to be used according to the needs of the application. For example: if the size of the elements is fixed and can be known in advance, we should use Array instead of ArrayList.     

    2. Some collection classes allow specifying the initial capacity. Therefore, if we can estimate the number of elements stored, we can set the initial capacity to avoid recalculating the hash value or scaling        

    3. Always use generics for type safety, readability and robustness reasons. At the same time, using generics can also avoid ClassCastException at runtime.

    4. Using the immutable class provided by JDK as the key of the Map can avoid implementing hashCode() and equals() methods for our own classes.
    5. Interface is better than implementation when programming.

    6. If the underlying collection is actually empty, return a collection or array with a length of 0, do not return null.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325221723&siteId=291194637