Java interview questions:
1. Core Java
2. OOP & Design Patterns Simplifiedcomputer.org 9178473568
3. Multithreading & Concurrency
4. Collections & Generics
5. Java 8+ Features
6. DSA in Java
7. System Design (Java)
1. Core Java Interview Questions
1. What is the difference between == and .equals() in Java?
2. What is the role of final, finally, and finalize() in Java?
3. What is autoboxing and unboxing in Java?
4. Explain JVM, JRE, and JDK.
5. How does garbage collection work in Java?
6. What is the difference between checked and unchecked exceptions?
7. Why is Java platform-independent?
8. What is the difference between String, StringBuilder, and StringBuffer?
9. What is the use of the transient keyword?
10. Can a constructor be private? What is the use case?
2. Object-Oriented Design (OOP)
1. Design a Parking Lot using OOP principles.
2. Explain SOLID principles with Java examples.
3. What is the difference between abstraction and encapsulation?
4. When would you use an interface instead of an abstract class?
5. How would you design an Elevator System in Java?
3. Multithreading & Concurrency
1. What is the difference between synchronized method and block?
2. How does wait(), notify(), and notifyAll() work?
3. What is a deadlock? How do you prevent it in Java?
4. What is the Executor framework? How is it better than creating threads manually?
5. Difference between volatile and AtomicInteger?
4. Java Collections & Generics
1. Difference between ArrayList and LinkedList?
2. How does HashMap work internally?
3. What are the differences between HashSet, TreeSet, and LinkedHashSet?
4. What is the difference between Comparable and Comparator?
5. Explain Generics — and wildcard <?>, <T extends Number> etc.
5. Java 8+ Features
1. What is a lambda expression?
2. How do you use Stream API for filtering and mapping?
3. What are method references in Java 8?
4. What is the Optional class and how does it prevent NullPointerException?
5. What is the difference between map() and flatMap() in streams?
6. DSA in Java (Algorithmic Coding Questions)
1. Find the longest substring without repeating characters.
2. Detect a cycle in a linked list.
3. Implement LRU Cache using Java.
4. Find the Kth largest element in an array.
5. Group anagrams from a list of strings.
6. Merge K sorted linked lists.
7. Implement a Trie (Prefix Tree) in Java.
8. Check if a binary tree is a valid BST.
9. Rotate a matrix by 90 degrees.
10. Solve Sudoku using backtracking.
7. System Design Questions (Java-specific)
1. Design a URL Shortener like bit.ly
2. Design a Messaging Queue (Pub-Sub system)
3. Design a rate limiter using token bucket algorithm
4. Design a cache (LRU, LFU)
5. Design a file storage system (like Dropbox)
Core Java Interview Questions with
Solutions
Q1: What is the difference between == and .equals() in Java?
• == compares object references (memory addresses).
• .equals() compares object content (value equality).
java
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // false (different objects)
System.out.println(a.equals(b)); // true (same content)
Q2: What is the role of final, finally, and finalize() in Java?
• final: Makes a variable constant, a method non-overridable, or a class non-inheritable.
• finally: A block that always executes after a try-catch block, typically for resource
cleanup.
• finalize(): A deprecated method called before garbage collection.
Q3: What is autoboxing and unboxing in Java?
• Autoboxing: Automatic conversion of a primitive type to its wrapper class (e.g., int to
Integer).
• Unboxing: Automatic conversion of a wrapper class to its primitive type (e.g., Integer to
int).
java
int a = 5;
Integer obj = a; // autoboxing
int b = obj; // unboxing
Q4: Explain JVM, JRE, and JDK.
• JVM: Java Virtual Machine, executes Java bytecode.
• JRE: Java Runtime Environment, includes JVM and standard libraries.
• JDK: Java Development Kit, includes JRE and tools like javac and debugger.
Q5: How does garbage collection work in Java?
Java’s Garbage Collector automatically reclaims memory from objects with no references.
java
MyClass obj = new MyClass();
obj = null; // eligible for garbage collection
System.gc(); // requests garbage collection
Q6: What is the difference between checked and unchecked
exceptions?
• Checked: Compile-time exceptions (e.g., IOException, SQLException). Must be handled
or declared.
• Unchecked: Runtime exceptions (e.g., NullPointerException, ArithmeticException). Not
enforced by the compiler.
Q7: Why is Java platform-independent?
Java code compiles to bytecode, which runs on any JVM. JVMs exist for all major operating
systems, enabling “write once, run anywhere.”
Q8: What is the difference between String, StringBuilder,
and StringBuffer?
• String: Immutable, thread-safe, slowest due to creating new objects for modifications.
• StringBuilder: Mutable, not thread-safe, fastest for single-threaded operations.
• StringBuffer: Mutable, thread-safe, slower than StringBuilder due to synchronization.
• Difference Between String and StringBuffer in Java
Feature String StringBuffer
Immutable – once created, cannot
Mutability Mutable – contents can be modified
be changed
Thread-safe – all methods are
Thread Safety Not thread-safe
synchronized
Faster for modifications in multithreaded
Performance Slower if many modifications
environments
Best when you need to manipulate strings
Use Case Best for small, fixed strings
(append, insert, etc.)
Limited to operations like Includes append(), insert(), delete(),
Methods
concat(), substring() reverse()
Memory New object is created on each Same object is modified, efficient for
Usage change multiple changes
Introduced in Java 1.0 Java 1.0
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
System.out.println(sb); // Output: Hello World (changed)
String s = "Hello";
s.concat(" World");
System.out.println(s); // Output: Hello (not changed)
Q9: What is the use of the transient keyword?
The transient keyword prevents a variable from being serialized.
java
class User implements Serializable {
String name;
transient String password; // not serialized
}
Q10: Can a constructor be private? What is the use case?
Yes, a private constructor is used in:
• Singleton pattern.
• Factory methods.
• Preventing object instantiation.
java
class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {} // private constructor
public static Singleton getInstance() {
return instance;
}
}
Object-Oriented Design (OOP)
Q11: Design a Parking Lot using OOP principles
Classes:
• ParkingLot: Manages slots and parking logic.
• Vehicle (abstract): Base class for vehicles.
• Car, Bike, Truck: Extend Vehicle.
• Slot: Represents a parking slot.
• Ticket: Tracks parking details.
Concepts: Inheritance, Encapsulation, Polymorphism, Composition.
java
abstract class Vehicle {
String number;
Vehicle(String number) { this.number = number; }
}
class Car extends Vehicle { Car(String number) { super(number); } }
class ParkingSlot {
boolean isOccupied;
Vehicle vehicle;
}
class ParkingLot {
List<ParkingSlot> slots;
public boolean parkVehicle(Vehicle v) { /* allocate a slot */ return
true; }
}
Q12: Explain SOLID principles with Java examples
• S (Single Responsibility): A class has one responsibility (e.g., a Logger class only
handles logging).
• O (Open/Closed): Classes open for extension, closed for modification (e.g., use
interfaces/abstract classes).
• L (Liskov Substitution): Subclasses must be substitutable for their parent (e.g., Square
substitutable for Rectangle).
• I (Interface Segregation): Prefer specific interfaces over large ones (e.g., avoid forcing
classes to implement unrelated methods).
• D (Dependency Inversion): Depend on abstractions, not concrete classes (e.g., inject
dependencies via interfaces).
Q13: What is the difference between abstraction and
encapsulation?
• Abstraction: Hides implementation details using abstract classes or interfaces (e.g.,
Shape interface).
• Encapsulation: Hides internal state using private fields and public getters/setters.
Q14: When would you use an interface instead of an
abstract class?
Use an interface for:
• Multiple inheritance (classes can implement multiple interfaces).
• Defining capabilities (e.g., Runnable, Serializable).
• Providing default methods (Java 8+).
Use an abstract class for:
• Sharing common code among subclasses.
• Strong “is-a” relationships (e.g., Dog is an Animal).