1. What is the difference between final, finally, and finalize()?
final is a keyword used to define constants, prevent method overriding, or inheritance. finally is a block that always executes after try-catch. finalize() is a method invoked by the garbage collector before object destruction (now deprecated).
2. How does Java achieve memory management?
Java uses automatic garbage collection, managed by the JVM. It divides memory into Young, Old, and Permanent generations and uses algorithms like Mark-and-Sweep and G1 GC to reclaim unused objects.
3. What are the differences between synchronized and Lock?
synchronized is simpler and implicit, but less flexible. Lock (from java.util.concurrent.locks) provides more granular control, including features like try-locking, timed locks, and interruptible locks.
4. What is a deadlock and how can you prevent it?
A deadlock occurs when multiple threads are blocked forever, each waiting for the other to release a lock. Prevention strategies include consistent lock ordering, using tryLock(), and avoiding nested locking when possible.
5. What is the difference between HashMap, LinkedHashMap, and TreeMap?
HashMap is unordered and allows one null key. LinkedHashMap preserves insertion order. TreeMap maintains a sorted order based on keys, either naturally or via a comparator.
6. What is type erasure in Java generics?
Type erasure is the process by which generic type information is removed at runtime. This means that List<String> and List<Integer> are treated the same by the JVM, enabling backward compatibility with legacy code.
7. What are functional interfaces and how are they used?
A functional interface has exactly one abstract method. They are the foundation for lambda expressions and method references. Examples include Runnable, Function, Consumer, and Predicate.
8. What is the difference between map() and flatMap() in streams?
map() transforms each element in a stream. flatMap() flattens nested structures (like a stream of lists) into a single stream of elements.
9. Explain the Singleton pattern and how to implement it thread-safely.
The Singleton pattern ensures only one instance of a class exists. A thread-safe implementation can use double-checked locking with a volatile instance variable or the enum-based approach introduced in Java 5.
10. What is dependency injection and how is it implemented in Java?
Dependency injection is a design pattern that decouples object creation from its usage. It’s commonly implemented in Java using frameworks like Spring, often through annotations like @Autowired or constructor injection.
Search
Categories
Recent Posts
Top Git interview Questions for Experienced Developer
Speed-up tips and tricks for Windows 11
Top 15 Linux Commands used by Experienced Developer
Top SQL Interview Examples for Experienced Developer
Internal Working of Java HashMap
Recent Tags