Interview Questions

Get ready for your next interview with our comprehensive question library

Java Interview Questions

Filter by Difficulty

1.

Explain the difference between primitive and reference data types.

beginner

Primitive types store actual values directly in memory and include: byte, short, int, long, float, double, boolean, char. They are stored on the stack and have fixed memory allocation.
Reference types store references (memory addresses) to objects located in heap memory. Examples include classes, interfaces, arrays, and enums. When you assign a reference variable, you're copying the reference, not the actual object.

int x = 10; // primitive - stores value 10
String str = "Hello"; // reference - stores memory address of String object
2.

What is autoboxing and unboxing?

beginner

Autoboxing is automatic conversion of primitive types to their corresponding wrapper classes. Unboxing is the reverse process.

// Autoboxing
Integer num = 100; // int to Integer
List<Integer> list = new ArrayList<>();
list.add(50); // int automatically boxed to Integer
// Unboxing
Integer wrapper = 200;
int primitive = wrapper; // Integer to int
3.

Explain the difference between == and equals() method.

beginner
  • == operator: Compares references for objects (memory addresses) and values for primitives
  • equals() method: Compares the actual content/state of objects (when properly overridden)
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = "hello";
String s4 = "hello";
System.out.println(s1 == s2);        // false (different objects)
System.out.println(s1.equals(s2));   // true (same content)
System.out.println(s3 == s4);        // true (string pool)
4.

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

beginner
  • String: Immutable, thread-safe. Each modification creates a new object.
  • StringBuilder: Mutable, not thread-safe, faster for single-threaded string operations.
  • StringBuffer: Mutable, thread-safe (synchronized), slower due to synchronization overhead.
String str = "Hello";
str += " World"; // Creates new String object
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // Modifies existing buffer
5.

What are access modifiers in Java?

beginner
  • private: Accessible only within the same class
  • default (package-private): Accessible within the same package
  • protected: Accessible within same package and subclasses
  • public: Accessible from anywhere
6.

What is the difference between static and instance variables/methods?

beginner

Static members belong to the class itself, not to any instance. They're loaded when the class is first loaded and shared among all instances.
Instance members belong to specific object instances and require object creation to access.

class Example {
    static int staticVar = 0;     // Class variable
    int instanceVar = 0;          // Instance variable
    static void staticMethod() {  // Can't access instance members
        // staticVar++; // OK
        // instanceVar++; // Compilation error
    }
    void instanceMethod() {       // Can access both
        staticVar++;
        instanceVar++;
    }
}
7.

What are the four pillars of OOP?

beginner
  1. Encapsulation: Bundling data and methods together, hiding internal details
  2. Inheritance: Creating new classes based on existing classes
  3. Polymorphism: Objects of different types responding to same interface
  4. Abstraction: Hiding complex implementation details, showing only essential features
8.

What is the purpose of the super keyword?

beginner

The super keyword refers to the immediate parent class object and is used to:

  1. Call parent class constructor
  2. Access parent class methods
  3. Access parent class variables
class Parent {
    String name = "Parent";
    void display() { System.out.println("Parent display"); }
}
class Child extends Parent {
    String name = "Child";
    Child() {
        super(); // Call parent constructor
    }
    void display() {
        super.display(); // Call parent method
        System.out.println("Parent name: " + super.name);
    }
}
9.

What is the difference between ArrayList and LinkedList?

beginner

ArrayList:

  • Dynamic array implementation
  • Fast random access O(1)
  • Slow insertion/deletion in middle O(n)
  • Better for frequent access operations
    LinkedList:
  • Doubly-linked list implementation
  • Slow random access O(n)
  • Fast insertion/deletion O(1)
  • Better for frequent modification operations
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
// ArrayList better for
String element = arrayList.get(100); // O(1)
// LinkedList better for
linkedList.add(0, "first"); // O(1)
linkedList.remove(0);       // O(1)
10.

What is the difference between checked and unchecked exceptions?

beginner

Checked Exceptions: Must be caught or declared in method signature. Checked at compile-time.
Examples: IOException, SQLException, ClassNotFoundException
Unchecked Exceptions: Runtime exceptions that don't need to be caught or declared.
Examples: NullPointerException, ArrayIndexOutOfBoundsException, IllegalArgumentException

// Checked exception - must handle
public void readFile() throws IOException {
    FileReader file = new FileReader("file.txt");
}
// Unchecked exception - optional handling
public void divide(int a, int b) {
    int result = a / b; // May throw ArithmeticException
}
11.

What is the exception hierarchy in Java?

beginner
Throwable
├── Error (unchecked)
│   ├── OutOfMemoryError
│   └── StackOverflowError
└── Exception
    ├── Checked Exceptions
    │   ├── IOException
    │   └── SQLException
    └── RuntimeException (unchecked)
        ├── NullPointerException
        └── ArrayIndexOutOfBoundsException
12.

What is the difference between throw and throws?

beginner

throw: Used to explicitly throw an exception from method or code block.
throws: Used in method signature to declare that method might throw certain exceptions.

public void validateAge(int age) throws IllegalArgumentException {
    if (age < 0) {
        throw new IllegalArgumentException("Age cannot be negative");
    }
}
13.

Can we have multiple catch blocks for a single try block?

beginner

Yes, multiple catch blocks are allowed. They are evaluated in order, so more specific exceptions should come before general ones.

try {
    // risky code
} catch (FileNotFoundException e) {
    // handle file not found
} catch (IOException e) {
    // handle other IO exceptions
} catch (Exception e) {
    // handle any other exception
}
14.

What is the difference between process and thread?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
15.

How can you create a thread in Java?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
16.

What is the difference between start() and run() methods?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
17.

What is the difference between stack and heap memory?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What is the difference between InputStream/OutputStream and Reader/Writer?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What is Spring Boot and its advantages?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
20.

What is the difference between @Component, @Service, @Repository, and @Controller?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 89 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime