1. What is the difference between == and .equals() in Java?

Answer:

  • == is used to compare the memory reference of objects.
  • .equals() is used to compare the content of objects.
  • For example: String str1 = new String("Java"); String str2 = new String("Java"); System.out.println(str1 == str2); // false System.out.println(str1.equals(str2)); // true

2. Explain the concept of JVM, JRE, and JDK.

Answer:

  • JVM (Java Virtual Machine): Executes Java bytecode.
  • JRE (Java Runtime Environment): Provides libraries and JVM for running Java applications.
  • JDK (Java Development Kit): Includes JRE and development tools for Java programming.

3. What are wrapper classes in Java?

Answer:

  • Wrapper classes provide object representations of primitive data types.
  • Example: int num = 10; Integer obj = Integer.valueOf(num);
  • Common wrapper classes: Integer, Double, Character, Boolean, etc.

4. What is the difference between ArrayList and LinkedList?

Answer:

FeatureArrayListLinkedList
ImplementationUses dynamic arrayUses doubly linked list
PerformanceFast in retrieval, slow in insertion/deletionSlow in retrieval, fast in insertion/deletion
Memory OverheadLessMore due to extra pointers

5. Explain method overloading and method overriding.

Answer:

  • Method Overloading: Multiple methods with the same name but different parameters within a class.
  • Method Overriding: A subclass redefines a method from the parent class.
  • Example: class Parent { void show() { System.out.println("Parent class method"); } } class Child extends Parent { void show() { System.out.println("Child class method"); } }

6. What is the difference between String, StringBuilder, and StringBuffer?

Answer:

FeatureStringStringBuilderStringBuffer
MutabilityImmutableMutableMutable
Thread SafetyNoNoYes
PerformanceSlowFastModerate

7. What is a Java interface, and how does it differ from an abstract class?

Answer:

  • Interface: A contract that defines abstract methods without implementation.
  • Abstract Class: Can have both abstract and concrete methods.
  • Example: interface Animal { void makeSound(); } class Dog implements Animal { public void makeSound() { System.out.println("Bark"); } }

8. Explain the concept of exception handling in Java.

Answer:

  • Java provides try, catch, finally, throw, and throws for handling exceptions.
  • Example: try { int a = 5 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); } finally { System.out.println("Execution completed"); }

9. What are Java annotations?

Answer:

  • Annotations provide metadata to code.
  • Common examples: @Override, @Deprecated, @FunctionalInterface.
  • Example: @Override public void display() { System.out.println("Overridden method"); }

10. What is the difference between HashMap and HashTable?

Answer:

FeatureHashMapHashTable
Thread SafetyNoYes
Null Keys/ValuesAllows one null key and multiple null valuesDoes not allow null keys or values
PerformanceFasterSlower

External Links