Prepare for Your Java Interview with These Essential Questions
This comprehensive guide covers 30 Java interview questions ranging from basic concepts to advanced topics. Whether you’re a fresher, have 1-3 years of experience, or are a seasoned developer with 3-6 years, these questions will help you prepare effectively for technical interviews at companies like Zoho, Paytm, and Atlassian.
Basic Java Interview Questions (1-10)
1. What is Java and why is it platform-independent?
Java is a high-level, object-oriented programming language that follows the “Write Once, Run Anywhere” principle. Java code is compiled into bytecode, which runs on any device with a Java Virtual Machine (JVM), making it platform-independent.
2. What is the difference between JDK, JRE, and JVM?
JDK (Java Development Kit) is for development and includes compiler and tools. JRE (Java Runtime Environment) is for running Java applications. JVM (Java Virtual Machine) executes bytecode and manages memory.
3. What are the four pillars of OOP in Java?
The four pillars are Encapsulation, Abstraction, Inheritance, and Polymorphism. These principles enable code reusability, maintainability, and flexibility.
4. What is the difference between == and equals() in Java?
The == operator compares object references (memory addresses), while equals() compares object content. For String class, equals() compares the actual string values.
5. What is the purpose of the main() method in Java?
The public static void main(String[] args) method is the entry point for Java applications. It must be public, static, return void, and accept String array arguments for JVM execution.
6. Explain access modifiers in Java.
Java has four access modifiers: public (accessible everywhere), protected (same package + subclasses), default/package-private (same package), and private (same class only).
7. What is the difference between method overloading and overriding?
Overloading occurs within the same class with same method name but different parameters. Overriding occurs in subclasses to provide specific implementation of superclass methods.
8. What are constructors in Java?
Constructors are special methods called when an object is created. They have the same name as the class, no return type, and can be parameterized or default.
public class Employee {
private String name;
// Default constructor
public Employee() {
this.name = "Unknown";
}
// Parameterized constructor
public Employee(String name) {
this.name = name;
}
}
9. What is an interface in Java?
An interface defines a contract of methods that implementing classes must provide. From Java 8, interfaces can have default and static methods.
10. Can a constructor be private in Java?
Yes, private constructors prevent instantiation from outside the class, commonly used in Singleton pattern or utility classes.
Intermediate Java Interview Questions (11-20)
11. What is the difference between ArrayList and LinkedList?
ArrayList uses dynamic arrays (fast random access, slow insertions/deletions). LinkedList uses doubly-linked nodes (fast insertions/deletions, slow random access).
12. Explain the difference between checked and unchecked exceptions.
Checked exceptions (like IOException) are checked at compile-time and must be handled. Unchecked exceptions (like NullPointerException) occur at runtime.
13. What is the try-with-resources statement?
Try-with-resources automatically closes resources (like file streams) that implement AutoCloseable, eliminating manual close() calls and preventing resource leaks.
try (FileInputStream fis = new FileInputStream("file.txt")) {
// Use resource
} // fis automatically closed here
14. What is garbage collection in Java?
Garbage collection automatically deallocates memory for objects no longer referenced. JVM uses algorithms like Mark-and-Sweep to identify and reclaim memory.
15. Explain String immutability in Java.
String objects are immutable – once created, their value cannot change. This ensures thread-safety and enables String pooling for memory efficiency.
16. What is the difference between HashMap and Hashtable?
HashMap is not synchronized (faster), allows one null key and multiple null values. Hashtable is synchronized (thread-safe), doesn’t allow null keys/values.
17. What are Java 8 Lambda Expressions?
Lambdas provide concise anonymous function syntax: (parameters) -> expression. Used primarily with functional interfaces and Stream API.
List names = Arrays.asList("Alice", "Bob");
names.forEach(name -> System.out.println(name));
18. What is the Stream API in Java 8?
Stream API enables functional-style operations on collections: filter, map, reduce, etc. Streams are lazy and can be parallelized for performance.
19. Explain the difference between Comparable and Comparator.
Comparable (natural ordering) implements compareTo() in the class. Comparator (custom ordering) is external and implements compare().
20. What is serialization in Java?
Serialization converts object state to byte stream for storage/transmission. Classes implement Serializable interface; transient fields are excluded.
Advanced Java Interview Questions (21-30)
21. What is the volatile keyword in Java?
volatile ensures variable visibility across threads and prevents compiler/JVM optimizations that reorder instructions, maintaining happens-before relationship.
22. Explain the difference between synchronized and ReentrantLock.
synchronized is built-in, simpler but less flexible. ReentrantLock provides tryLock(), fairness, condition variables, and multiple lock support.
23. What is a deadlock in Java multithreading?
Deadlock occurs when two or more threads wait indefinitely for each other to release locks. Prevention strategies include lock ordering and timeouts.
24. What is the Liskov Substitution Principle?
Subtypes must be substitutable for their base types without altering program correctness. Violating LSP breaks polymorphism and inheritance benefits.
25. Explain the Singleton design pattern.
Singleton ensures only one instance exists. Use enum, double-checked locking, or static initialization for thread-safety.
public enum DatabaseConnection {
INSTANCE;
public void connect() {
// Connection logic
}
}
26. What is the difference between Fail-Fast and Fail-Safe iterators?
Fail-Fast (ArrayList) throws ConcurrentModificationException on structural changes. Fail-Safe (CopyOnWriteArrayList) works on copy, no exception.
27. Explain the Proxy design pattern.
Proxy controls access to an object (real subject). Used for lazy loading, access control, remote proxies, and virtual proxies in frameworks like Hibernate.
28. What is the Template Method design pattern?
Defines algorithm skeleton in superclass, allowing subclasses to override specific steps while keeping structure fixed. Common in framework callbacks.
29. How does Java handle the diamond problem in multiple inheritance?
Java doesn’t support multiple class inheritance but allows multiple interfaces. Java 8+ resolves method conflicts through explicit override in implementing class.
30. What is Dependency Injection and how is it implemented?
Dependency Injection provides dependencies externally rather than creating them internally. Implemented via constructor, setter, or field injection for loose coupling.
Master these 30 Java interview questions to confidently tackle technical interviews across experience levels. Practice coding examples and understand core concepts thoroughly for success.