Sunday, March 12, 2023

Java Interview Question

Java Interview Question

1. What is the difference between an abstract class and an interface?

Answer: An abstract class can have both abstract and non-abstract methods, whereas an interface can only have abstract methods. An abstract class can also have constructors, instance variables, and method implementations, whereas an interface cannot.

Abstract Class Example 

public abstract class Vehicles {
   private int numOfWheels;
   
   public Vehicles(int numOfWheels) {
      this.numOfWheels = numOfWheels;
   }
   
   public int getNumOfWheels() {
      return this.numOfWheels;
   }
   
   public abstract void drive();
}

Interface Example

public interface Drivable {
   void drive();
}

2. What is the difference between a HashMap and a Hashtable?

Answer: HashMap is not thread-safe, whereas Hashtable is thread-safe. HashMap allows null values for both keys and values, whereas Hashtable does not allow null keys or values.

Example :

Creating a HashMap and adding elements

HashMap<String, Integer> myHashMap = new HashMap<>();
myHashMap.put("John", 25);
myHashMap.put("Ravi", 30);
int value = myHashMap.get("John");

Creating a Hashtable and adding elements

Hashtable<String, Integer> myHashTbl = new Hashtable<>();
myHashTbl.put("John", 25);
myHashTbl.put("Ravi", 30);
int value = myHashTbl.get("John");

3. What is the difference between an inner class and a nested class?

Answer: An inner class is a non-static nested class, whereas a nested class can be either static or non-static. An inner class has access to the instance variables and methods of the enclosing class, whereas a static nested class does not.

Example :

Inner class 

public class OuterClass {

   private int x;
   
   public class InnerClass {
      public void printX() {
         System.out.println(x);
      }
   }

Nested class example

   public static class NestedClass {
      public void printHello() {
         System.out.println("Hello Java-Scholars");
      }
   }
}

4. What is the difference between a StringBuilder and a StringBuffer?

Answer: StringBuilder is not thread-safe, whereas StringBuffer is thread-safe. StringBuilder is faster because it is not synchronized, whereas StringBuffer is slower because it is synchronized.

Example:

Using StringBuilder

StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("Java-Scholars");
String result = sb.toString();
Using StringBuffer
StringBuffer sb = new StringBuffer();
sb.append("Hello");
sb.append(" ");
sb.append("Java-Scholars");
String result = sb.toString();

5. What is the difference between an ArrayList and a LinkedList?

Answer: ArrayList is implemented as a dynamic array, whereas LinkedList is implemented as a doubly-linked list. ArrayList is faster for random access and iteration, whereas LinkedList is faster for inserting and removing elements.

Example :

Creating an ArrayList and adding elements

ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(1);
myList.add(2);

Creating a LinkedList and adding elements

LinkedList<Integer> myList = new LinkedList<Integer>();
myList.add(1);
myList.add(2);

6. What is the difference between a checked and an unchecked exception?

Answer: A checked exception is a type of exception that must be declared in the method signature or caught in a try-catch block. An unchecked exception is a type of exception that does not need to be declared or caught.

Example :

Checked exception

public void myMethod() throws IOException {
   // Code here
}

Unchecked exception

public void myMethod() {
   throw new NullPointerException();
}

0 comments:

Post a Comment