Monday, April 29, 2024

Core Java interview question and answer

Core Java interview question and answer:
  1. What is JVM, JRE, and JDK?
  2. Java Time API
    1. The most commonly used classes are LocalDate, LocalTime and LocalDateTime. As their names indicate, they represent the local date/time from the context of the observer.
    2. We mainly use these classes when time zones are not required to be explicitly specified in the context. 
  3. Differences between static and default methods in Java 8:
    1. Default methods can be overridden in implementing class, while static cannot.
    2. A static method belongs only to the Interface class, so you can only invoke a static method on the Interface class, not on the class implementing this Interface:
    3. Both class and interface can have static methods with the same names, and neither overrides the other.
  4. Java list interface
    1. Java List Interface is an interface in Java's Collection Framework, located in Java.util package. It extends the Collection interface.
    2. Allows duplicate elements
    3. Preserves insertion order
    4. Allows null elements (can have multiple null values)
    5. List Interface Methods
      1. list.add("Java");
      2. list.remove("Java");
      3. String element = list.get(0);
      4. int size = list.size();
      5. boolean exists = list.contains("HTML");
    6. Classes implementing list interface in Java
      1. ArrayList is a resizable array that grows automatically when new items are added and shrinks when items are removed. It provides fast access to elements using indices but may be slower in operations, such as insertion or deletion, which require shifting elements. List arrayList = new ArrayList<>();
      2. LinkedList is implemented as a double-linked list where each node holds the data and connections to the next and previous nodes. It offers efficient insertions or deletions but slower access to elements since it has to traverse the list. List linkedList = new LinkedList<>();
      3. Vector is similar to ArrayList but is thread-safe. It provides synchronised methods to ensure only one thread can access the vector at a time. List vector = new Vector<>();
      4. Stack is a class that implements a last-in-first-out (LIFO) data structure. It extends Vector to provide push and pop operations. List stack = new Stack<>();
  5. What is Recursion in Java
    1. A method in Java that calls itself is called a recursive method.
  6. What is the difference between System.out, System.err, and System.in?
  7. If you have a Java test class that implements interfaces A and B, both of which have a default method print(), then how to call the print() method specifically from interface A?
    1. A.super.print();
  8. Interface vs  Abstract class ?
    1. Abstract class can have abstract and non-abstract methods.  Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
    2. Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
    3. An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only.
  9. Difference between Comparable and Comparator?
    1. Comparable:
      1. The Comparable interface is used to define the natural ordering of objects.
      2. It contains only one method: compareTo(Object obj), which compares the current object with the specified object for order.
      3. Classes that implement Comparable can be sorted automatically by collections like Arrays.sort() or Collections.sort().
      4. Collections.sort(students);
    2. Comparator:
      1. The Comparator interface is used to define custom ordering of objects.
      2. It contains two methods: compare(Object obj1, Object obj2) and equals(Object obj).
      3. The compare() method compares its two arguments for order. It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
      4. Comparator is particularly useful when you want to sort objects based on different criteria without modifying their original class
      5. Collections.sort(students, new StudentNameComparator());
  10. Garbage collector
    1. A garbage collector (GC) is responsible for automatically managing the memory by reclaiming memory that is no longer in use. 
    2. The young generation is the place where all the new objects are created. When the young generation is filled, garbage collection is performed. This garbage collection is called Minor GC. Young Generation is divided into three parts - Eden Memory and two Survivor Memory spaces.
  11. BigInteger
    1. BigInteger objects are constructed using strings to ensure accuracy for large numbers.
    2. BigInteger num1 = new BigInteger("123456789012345678901234567890");
  12. Cloneable interface
    1. Cloneable interface is used to enable a class to support the cloning of its objects, but it does not provide any guidance on how the cloning should be done. It is the responsibility of the class to implement the clone method properly.
  13. Shallow Copy
    1. In a shallow copy, a new object is created, but it only copies the references of the original object's fields. It doesn't create new instances of the objects referenced by the original object.
    2. Changes made to the copied object will affect the original object, and vice versa if the fields are mutable.
      @Override
      public Object clone() throws CloneNotSupportedException {
      return super.clone();
      }
  14. Deep Copy
    1. In a deep copy, not only the object itself but also all the objects referenced directly or indirectly by it are copied to a new object.
    2. Changes made to the copied object will not affect the original object, and vice versa.
      @Override
      public Object clone() throws CloneNotSupportedException {
      Student cloned = (Student) super.clone();
      cloned.courses = new ArrayList<>(this.courses);
      return cloned;
      }
  15. Marker interface
    1. A marker interface in Java is an interface that doesn't declare any methods or fields but is used to mark or tag a class as having some special behaviour or characteristic. Marker interfaces are also known as tag interfaces.
  16. Throw, Throws  AND  Throwable 
    1. Throw is a keyword used to explicitly throw an exception in your code.
    2. Throws is a keyword used in a method declaration to specify that the method may throw one or more exceptions.
    3. Throwable as the base class for all exceptions and errors in Java. 

No comments:

Post a Comment