Prepare for your next Java interview with these 30 carefully curated questions covering basic, intermediate, and advanced topics. Whether you’re a fresher, have 1-3 years of experience, or are a seasoned developer with 3-6 years, this guide provides clear explanations and code examples to help you succeed.
Basic Java Interview Questions (1-10)
1. What is the difference between JDK, JRE, and JVM in Java?
The JDK (Java Development Kit) is for development and includes compiler and tools. The JRE (Java Runtime Environment) runs Java applications. The JVM (Java Virtual Machine) executes bytecode and provides platform independence.
2. Why is String immutable in Java?
String is immutable for security, thread-safety, and better performance with string pooling. Once created, its value cannot change, preventing unintended modifications in multi-threaded environments.
3. What is the difference between == and equals() in Java?
The == operator compares references (memory addresses), while equals() compares content values. For strings, use equals() to check actual string content.
4. Explain the purpose of the main() method in Java.
The main() method is the entry point for Java applications. It must be public, static, void, and named main(String[] args) for the JVM to invoke it.
5. What are the differences between ArrayList and LinkedList?
ArrayList uses a dynamic array (fast random access), while LinkedList uses a doubly-linked list (fast insertions/deletions). Use ArrayList for frequent reads, LinkedList for modifications.
6. What is the difference between method overloading and overriding?
Overloading occurs in the same class with different parameters (compile-time). Overriding happens in subclasses with the same signature (runtime polymorphism).
7. How do if-else conditionals work in Java?
Conditionals execute code based on boolean tests. Here’s an example:
int score = 85;
if (score >= 90) {
System.out.println("Excellent");
} else if (score >= 75) {
System.out.println("Good");
} else {
System.out.println("Needs Improvement");
}
8. What is a try-catch block used for?
Try-catch handles exceptions: place risky code in try, and handle errors in catch. It prevents program crashes.
9. What is the finally block in Java?
Finally executes cleanup code (like closing resources) regardless of exceptions, always after try-catch.
10. What are the four pillars of OOP in Java?
Encapsulation (data hiding), Inheritance (reusing code), Polymorphism (multiple forms), Abstraction (hiding complexity).
Intermediate Java Interview Questions (11-20)
11. What is the difference between Comparable and Comparator?
Comparable has compareTo() for natural ordering (implements in class). Comparator provides custom sorting via compare() (external class).
12. Explain HashMap vs TreeMap in Java.
HashMap is unordered, faster O(1) access. TreeMap is sorted by keys, O(log n) operations, useful for ordered data.
13. What is a NullPointerException and how to avoid it?
NullPointerException occurs when accessing methods on null objects. Avoid by null checks or Optional class.
14. How do you reverse a string in Java?
Use StringBuilder reverse() method:
String str = "hello";
String reversed = new StringBuilder(str).reverse().toString(); // "olleh"
15. What are Lambda expressions in Java 8?
Lambdas enable functional programming: (params) -> expression. They simplify anonymous inner classes for functional interfaces.
16. Explain Streams API in Java 8 with an example.
Streams process collections functionally. Example filtering even numbers:
List<Integer> numbers = Arrays.asList(1,2,3,4);
numbers.stream().filter(n -> n%2==0).forEach(System.out::println); // 2,4
17. What is the difference between StringBuilder and StringBuffer?
StringBuilder is mutable and not thread-safe (faster). StringBuffer is mutable and thread-safe (slower).
18. Scenario: How would you handle exceptions in a file reading operation at Flipkart?
Use try-with-resources for auto-closing and catch IOException:
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
19. What is an Enum in Java?
Enum defines fixed constants like days of the week. It supports methods and constructors for type-safe constants.
20. Explain Garbage Collection in Java.
GC automatically reclaims memory from unreachable objects. JVM uses algorithms like G1 for efficient collection.
Advanced Java Interview Questions (21-30)
21. What is multithreading in Java and how to create threads?
Multithreading allows concurrent execution. Create via Thread class extension or Runnable interface implementation.
22. Implement a thread-safe Singleton pattern.
Use enum or synchronized method:
public class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
23. What is the synchronized keyword?
Synchronized ensures thread safety using monitor locks on methods or blocks, preventing concurrent access.
24. Scenario: Implement producer-consumer using wait() and notify() for Zoho’s queue system.
Use synchronized with shared queue and wait/notify for coordination between producer and consumer threads.
25. What is ExecutorService in Java?
ExecutorService manages thread pools for concurrent task execution, improving efficiency over manual threads.
26. Explain ConcurrentHashMap and its thread-safety.
ConcurrentHashMap allows concurrent reads/writes with segment locking, better than synchronized HashMap.
27. What is a deadlock and how to avoid it in Paytm’s payment processing?
Deadlock occurs when threads wait cyclically. Avoid by consistent lock ordering and timeouts.
28. What is Reflection in Java?
Reflection inspects/modifies classes at runtime, useful for frameworks but impacts performance.
29. Scenario: Use CompletableFuture for asynchronous processing at Salesforce.
CompletableFuture enables non-blocking async operations:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
future.thenAccept(System.out::println);
30. How would you debug a memory leak using tools at Atlassian?
Use VisualVM or JFR to profile heap, identify retained objects, and fix leaks from static references or improper caching.