1. What are the main features of Java?

Answer:

Java is a versatile, object-oriented programming language with several key features:

  • Platform Independence: Write once, run anywhere (WORA) using JVM.
  • Object-Oriented: Follows OOP principles like encapsulation, inheritance, and polymorphism.
  • Automatic Memory Management: Uses garbage collection to free unused memory.
  • Multi-threading Support: Enables concurrent programming using threads.
  • Security: Provides a secure execution environment.
  • Rich API and Libraries: Comes with a comprehensive standard library for various applications.

2. What is the difference between JDK, JRE, and JVM?

Answer:

  • JDK (Java Development Kit): A software development kit that includes JRE, compilers, and tools for developing Java applications.
  • JRE (Java Runtime Environment): Contains JVM and libraries required to run Java applications.
  • JVM (Java Virtual Machine): An abstract machine that executes Java bytecode.

3. Explain the difference between Stack and Heap memory in Java.

Answer:

  • Stack Memory: Stores method-specific local variables and function calls. It follows the Last In, First Out (LIFO) principle.
  • Heap Memory: Used for dynamic memory allocation where objects are stored.

Example:

public class MemoryDemo {
    public static void main(String[] args) {
        int a = 10; // Stored in Stack
        String str = new String("Hello"); // Stored in Heap
    }
}

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

Answer:

FeatureAbstract ClassInterface
MethodsCan have both abstract and concrete methodsOnly abstract methods (before Java 8)
VariablesCan have instance variablesOnly static and final variables
InheritanceCan extend another classCan implement multiple interfaces

Example:

abstract class Animal {
    abstract void sound();
}

interface Pet {
    void play();
}

5. Explain method overloading and method overriding in Java.

Answer:

  • Method Overloading: Defining multiple methods with the same name but different parameters.
  • Method Overriding: Redefining a method in a subclass that already exists in the parent class.

Example:

class Parent {
    void show() {
        System.out.println("Parent Class");
    }
}
class Child extends Parent {
    @Override
    void show() {
        System.out.println("Child Class");
    }
}

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

Answer:

  • == Operator: Compares memory addresses.
  • .equals() Method: Compares the actual values.

Example:

String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true

7. What is the purpose of the ‘final’ keyword in Java?

Answer:

The final keyword can be used in three ways:

  • Final Variable: Value cannot be changed.
  • Final Method: Cannot be overridden.
  • Final Class: Cannot be extended.

Example:

final class Constants {
    final int MAX_VALUE = 100;
}

8. What are checked and unchecked exceptions in Java?

Answer:

  • Checked Exceptions: Must be handled at compile-time (e.g., IOException, SQLException).
  • Unchecked Exceptions: Occur at runtime and do not require handling (e.g., NullPointerException, ArithmeticException).

Example:

try {
    FileReader file = new FileReader("test.txt"); // Checked Exception
} catch (IOException e) {
    e.printStackTrace();
}

9. Explain the difference between ArrayList and LinkedList.

Answer:

FeatureArrayListLinkedList
ImplementationUses dynamic arrayUses doubly linked list
Insertion/DeletionSlowFast
Access TimeFastSlow

Example:

ArrayList<String> list = new ArrayList<>();
list.add("Java");

10. What is the purpose of the volatile keyword in Java?

Answer:

The volatile keyword ensures that the value of a variable is always read from main memory, preventing thread-local caching.

Example:

class SharedResource {
    volatile int counter = 0;
}

External Resources