☕ Core Java Interview Questions 2026
Core Java Interview Questions, OOPs, Collections, Multi-threading, and JVM Architecture.
All (20)
🟢 Easy
🟡 Medium
🔴 Hard
1
What is the difference between JDK, JRE, and JVM?
Easy
▼
JDK (Java Development Kit): Complete toolkit for developers. Includes compiler (javac), JRE, debugger, and development tools.
JRE (Java Runtime Environment): Required to RUN Java programs. Includes JVM + core libraries. No compiler.
JVM (Java Virtual Machine): Executes bytecode. Platform-specific but makes Java platform-independent.
Relation: JDK ⊃ JRE ⊃ JVM
2
What are the 4 pillars of OOP in Java?
Easy
▼
1. Encapsulation: Wrapping data + methods into a class. Use private fields + public getters/setters.
2. Inheritance: Child class acquires properties of parent class using
extends keyword.
3. Polymorphism: Same method behaves differently. Two types:
- Compile-time (Method Overloading)
- Runtime (Method Overriding)
4. Abstraction: Hiding implementation details. Achieved via abstract classes and interfaces.
3
What is the difference between == and .equals() in Java?
Easy
▼
== operator: Compares object references (memory address), not content.
.equals(): Compares actual content/values.
Example:
String a = new String("hello");
String b = new String("hello");
a == b // false (different objects)
a.equals(b) // true (same content)
Note: For String literals, == may work due to String Pool, but always use .equals() for safe comparison.
4
What is the difference between ArrayList and LinkedList?
Easy
▼
ArrayList:
- Dynamic array internally
- Fast random access O(1)
- Slow insert/delete in middle O(n)
- Less memory overhead
LinkedList:
- Doubly linked list internally
- Slow random access O(n)
- Fast insert/delete O(1)
- More memory (stores prev/next pointers)
Use ArrayList when you read frequently.
Use LinkedList when you insert/delete frequently.
5
What is method overloading vs method overriding?
Easy
▼
Method Overloading (Compile-time Polymorphism):
- Same method name, different parameters
- Happens in same class
- Resolved at compile time
int add(int a, int b)
double add(double a, double b)
Method Overriding (Runtime Polymorphism):
- Same method signature in parent + child class
- Child class provides specific implementation
- Uses @Override annotation
- Resolved at runtime
6
What is the difference between abstract class and interface?
Medium
▼
Abstract Class:
- Can have both abstract and concrete methods
- Can have instance variables
- Supports constructors
- Single inheritance only
- Use when classes share code
Interface:
- All methods abstract by default (Java 7)
- Java 8+ allows default/static methods
- Only public static final variables
- Multiple interface implementation allowed
- Use to define a contract/behavior
7
Explain the concept of Java Collections Framework.
Medium
▼
Java Collections Framework provides a unified architecture to store and manipulate groups of objects.
Key Interfaces:
- List — ordered, allows duplicates (ArrayList, LinkedList)
- Set — no duplicates (HashSet, TreeSet, LinkedHashSet)
- Map — key-value pairs (HashMap, TreeMap, LinkedHashMap)
- Queue — FIFO structure (PriorityQueue, LinkedList)
Root interface: Collection (except Map)
8
What is multithreading? How do you create a thread in Java?
Medium
▼
Multithreading: Executing multiple threads simultaneously to maximize CPU usage.
Two ways to create a thread:
1. Extend Thread class:
class MyThread extends Thread {
public void run() { System.out.println("Thread running"); }
}
new MyThread().start();
2. Implement Runnable interface (Preferred):
class MyTask implements Runnable {
public void run() { System.out.println("Running"); }
}
new Thread(new MyTask()).start();
9
What is the difference between HashMap and Hashtable?
Medium
▼
HashMap:
- Not synchronized (not thread-safe)
- Allows one null key and multiple null values
- Faster performance
- Part of Java 1.2 Collections
Hashtable:
- Synchronized (thread-safe)
- Does not allow any null key or value
- Slower due to synchronization
- Legacy class (Java 1.0)
Tip: Use
ConcurrentHashMap instead of Hashtable for thread-safe operations.
10
What is the Java Memory Model? Explain Heap and Stack.
Medium
▼
Stack Memory:
- Stores method calls, local variables, references
- LIFO order
- Thread-specific (each thread has its own stack)
- Automatically freed when method returns
- StackOverflowError when full
Heap Memory:
- Stores all objects and class instances
- Shared across threads
- Managed by Garbage Collector
- OutOfMemoryError when full
String Pool is a special area in heap for string literals.
11
What are Java 8 Streams? Give an example.
Medium
▼
Java 8 Streams provide a declarative way to process collections of data.
Key Operations:
- Intermediate: filter(), map(), sorted(), distinct()
- Terminal: collect(), forEach(), count(), reduce()
Example — filter even numbers:
List nums = Arrays.asList(1,2,3,4,5,6);
List evens = nums.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
// Output: [2, 4, 6]
12
What is a Lambda Expression in Java 8?
Easy
▼
A lambda expression is a short block of code that takes parameters and returns a value. It implements a functional interface.
Syntax: (parameters) -> expression
Without Lambda:
Runnable r = new Runnable() {
public void run() { System.out.println("Hello"); }
};
With Lambda:
Runnable r = () -> System.out.println("Hello");
Common use: With Collections.sort(), Stream API, event handlers.
13
What is the difference between final, finally, and finalize?
Easy
▼
final: Keyword
- Variable: value cannot change (constant)
- Method: cannot be overridden
- Class: cannot be extended
finally: Block in try-catch that always executes (for cleanup).
try { } catch(e) { } finally { // always runs }
finalize(): Method called by GC before object is garbage collected. Deprecated in Java 9+.
14
What is Exception Handling in Java? What is checked vs unchecked exception?
Easy
▼
Exception Handling keywords: try, catch, finally, throw, throws
Checked Exceptions:
- Checked at compile time
- Must be handled or declared
- Examples: IOException, SQLException, FileNotFoundException
Unchecked Exceptions (RuntimeException):
- Checked at runtime
- No mandatory handling
- Examples: NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException
15
What is Garbage Collection in Java?
Medium
▼
Garbage Collection (GC) is the process of automatically reclaiming heap memory occupied by objects that are no longer referenced.
How it works:
- JVM tracks object references
- Objects with no references become eligible for GC
- GC runs in background (daemon thread)
GC Algorithms: Serial GC, Parallel GC, G1 GC (default Java 9+), ZGC
You cannot force GC, but can request:
System.gc() (not guaranteed)
16
What is the Singleton Design Pattern? Implement it in Java.
Hard
▼
Singleton ensures only ONE instance of a class exists throughout the application.
Thread-safe implementation:
public class Singleton {
private static volatile Singleton instance;
private Singleton() { } // private constructor
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
Use cases: Database connection, Logger, Configuration manager
17
What is the difference between String, StringBuilder, and StringBuffer?
Easy
▼
String:
- Immutable — any change creates new object
- Thread-safe (immutable)
- Stored in String Pool
StringBuilder:
- Mutable — in-place modifications
- NOT thread-safe
- Faster than StringBuffer
- Use for single-thread string manipulation
StringBuffer:
- Mutable — in-place modifications
- Thread-safe (synchronized)
- Slower than StringBuilder
- Use in multi-threaded environment
18
What is Java Reflection API?
Hard
▼
Reflection allows a program to inspect and modify its own structure and behavior at runtime.
Capabilities:
- Get class name, methods, fields, constructors
- Invoke methods dynamically
- Access private members
- Create objects without knowing the class at compile time
Example:
Class> cls = Class.forName("java.lang.String");
Method[] methods = cls.getMethods();
for(Method m : methods) System.out.println(m.getName());
Used in: Frameworks (Spring, Hibernate), IDEs, Testing tools
19
What are generics in Java and why are they used?
Medium
▼
Generics allow you to write type-safe code that works with different data types without casting.
Benefits:
- Type safety at compile time
- Eliminates ClassCastException
- Code reusability
Generic Class:
class Box {
T value;
Box(T v) { this.value = v; }
T get() { return value; }
}
Box intBox = new Box<>(42);
Box strBox = new Box<>("Hello");
Wildcards: ? extends T (upper bound),
? super T (lower bound)
20
What is the difference between Comparable and Comparator in Java?
Hard
▼
Comparable (java.lang):
- Interface with compareTo() method
- Class itself defines its natural ordering
- Modifies the actual class
class Student implements Comparable {
public int compareTo(Student s) { return this.age - s.age; }
}
Comparator (java.util):
- Interface with compare() method
- External class defines custom ordering
- Multiple sort orders possible without modifying class
Collections.sort(list, (a,b) -> a.name.compareTo(b.name));