Basic Java Interview Questions (1-10)
These foundational questions test core Java concepts suitable for freshers and beginners.
1. What is the difference between JDK, JRE, and JVM?
JDK (Java Development Kit) is for development and includes compiler tools. JRE (Java Runtime Environment) runs Java applications. JVM (Java Virtual Machine) executes bytecode and provides platform independence.[2]
2. Why is String immutable in Java?
String is immutable for security, thread-safety, and optimization in hash-based collections like HashMap. Once created, its value cannot change.[2]
3. What is the difference between == and equals() in Java?
== compares object references (memory addresses). equals() compares content values, and String overrides it for content comparison.[2]
4. Explain the four pillars of OOP in Java.
Encapsulation hides data using access modifiers. Inheritance allows subclassing. Polymorphism enables method overriding. Abstraction uses abstract classes/interfaces.[5]
5. What are access modifiers in Java?
public: accessible everywhere. protected: within package and subclasses. default (no modifier): package-private. private: class only.[5]
6. What is method overloading?
Method overloading allows multiple methods with the same name but different parameters in the same class. It enables static polymorphism.[6]
7. What is the purpose of the main method in Java?
public static void main(String[] args) is the entry point for Java applications. JVM calls it to start execution.[2]
8. Difference between ArrayList and array in Java?
Arrays have fixed size. ArrayList is dynamic, resizes automatically, and is part of Collections framework.[3]
9. What is autoboxing and unboxing?
Autoboxing converts primitives to wrappers (int to Integer). Unboxing converts wrappers to primitives automatically.[2]
10. Explain try-catch-finally blocks.
try contains risky code. catch handles exceptions. finally executes always for cleanup like closing resources.[4]
Intermediate Java Interview Questions (11-20)
These questions target candidates with 1-3 years experience, focusing on practical usage.
11. What is the difference between String, StringBuilder, and StringBuffer?
String is immutable. StringBuilder is mutable and not thread-safe. StringBuffer is mutable and thread-safe.[4]
12. Explain Comparable vs Comparator.
Comparable has compareTo() for natural ordering (implements interface). Comparator provides custom ordering via separate class.[2]
13. What are Java 8 Lambda expressions?
Lambdas enable functional programming: (params) -> expression. Used with functional interfaces like Predicate.[1]
Predicate<Integer> isEven = x -> x % 2 == 0;
14. What is method overriding?
Subclass provides specific implementation of superclass method with same signature. Enables runtime polymorphism.[6]
15. How does HashMap work internally?
HashMap uses array of buckets. hashCode() determines bucket, equals() checks keys. Handles collisions with linked lists.[3]
16. What is exception propagation in Java?
Unchecked exceptions propagate up call stack until caught or terminate program. Checked must be handled or declared.[4]
17. Explain static vs instance variables.
Static belongs to class, shared across instances. Instance belongs to object, unique per instance.[2]
18. What is the Collections Framework?
Unified architecture for lists, sets, maps. Includes ArrayList, HashSet, TreeMap etc., with algorithms like sort.[5]
19. Scenario: Reverse a string without built-in methods at Flipkart.
Use two pointers or StringBuilder append in reverse loop.
public String reverse(String str) {
StringBuilder sb = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
sb.append(str.charAt(i));
}
return sb.toString();
}
[1]
20. What is Garbage Collection in Java?
JVM automatically frees memory of unreferenced objects. Uses algorithms like Mark-Sweep for heap cleanup.[3]
Advanced Java Interview Questions (21-30)
These challenge 3-6 years experienced developers on concurrency and internals, relevant at companies like Atlassian or Zoho.
21. What is a thread-safe Singleton pattern?
Use enum or double-checked locking with volatile for lazy initialization in multithreaded environments.[1]
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;
}
}
22. Explain synchronized keyword.
synchronized on method/block acquires monitor lock for thread safety, preventing concurrent access.[2]
23. What is ExecutorService?
Framework for managing thread pools. submit() tasks, shutdown() gracefully. Better than manual Thread creation.[1]
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(() -> System.out.println("Task"));
executor.shutdown();
24. Producer-Consumer using wait-notify at Paytm.
Producer adds to bounded queue using notify(). Consumer waits() if empty, notifies after consume.[1]
25. What is ConcurrentHashMap?
Thread-safe HashMap with segment locking for high concurrency. Allows concurrent reads/writes.[1]
26. Explain volatile keyword.
volatile ensures variable visibility across threads, prevents caching in thread-local memory.[6]
27. How to avoid deadlock?
Impose ordering on lock acquisition, use tryLock(), avoid nested locks. Detect with thread dumps.[1]
28. What is CompletableFuture?
Asynchronous programming: supplyAsync(), thenApply(), handle() for chaining non-blocking operations.[1]
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello")
.thenApply(s -> s + " World");
29. Scenario: Implement thread-safe cache using ConcurrentHashMap at Salesforce.
Use ConcurrentHashMap<Key, Value>. putIfAbsent() ensures atomic insertion without external locks.[1]
30. What is JIT compiler?
Just-In-Time compiler optimizes bytecode to native code at runtime for performance. HotSpot uses it.[2]