Top Java Interview Questions for 5 Years Experience
At 5 years of experience, interviews shift from basic syntax to deeper design, performance, and real-world problem-solving. Here are commonly asked questions at this level.
1. How does HashMap work internally?
HashMap stores entries in buckets based on the hash code of the key. Since Java 8, buckets with many collisions convert from a linked list to a balanced tree for better performance.
2. What is the difference between ArrayList and LinkedList in terms of performance?
ArrayList offers fast random access (O(1)) but slower inserts/deletes in the middle (O(n)). LinkedList has fast inserts/deletes (O(1)) but slower random access (O(n)).
3. Explain the difference between checked and unchecked exceptions.
Checked exceptions are verified at compile time and must be declared or handled; unchecked exceptions (subclasses of RuntimeException) are not checked at compile time.
4. What is the difference between synchronized methods and synchronized blocks?
A synchronized method locks the entire method on the object's monitor, while a synchronized block allows locking only a specific section of code, offering finer control.
5. How does garbage collection work in Java?
The JVM automatically identifies objects that are no longer reachable and reclaims their memory, using generational garbage collectors that separate young and old objects for efficiency.
6. What are functional interfaces, and how are they used with lambdas?
A functional interface has exactly one abstract method and can be implemented concisely using a lambda expression, such as Runnable or a custom interface.
7. How would you design a thread-safe Singleton class?
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;
}
}
8. What is the difference between abstract classes and interfaces (post Java 8)?
Interfaces can now have default and static methods but still can't hold instance state; abstract classes can maintain state and constructors, and support single inheritance only.
9. How do you handle N+1 query problems with JPA/Hibernate?
Common solutions include using fetch joins, entity graphs, or batch fetching to load related entities efficiently in a single query instead of many.
10. How would you design a rate limiter in Java?
Common approaches include token bucket or sliding window algorithms, often implemented with a concurrent data structure or backed by Redis in distributed systems.
Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.