How to Pass a Java Interview: Top Advanced-Level Questions
\n\nAn interview for a Senior Java Developer position is not just a syntax check. Interviewers want to ensure you understand how the JVM works internally, can design multithreaded systems, and know the intricacies of garbage collection. In this article, we will break down the most common and difficult questions to help you confidently pass a technical interview.
\n\n1. Multithreading and the Java Memory Model (JMM)
\nThis is one of the most extensive topics. Interviewers check whether you understand how threads interact through shared memory and what problems can arise.
\n\nQuestion: What is the difference between volatile and synchronized?
\nAnswer: volatile guarantees visibility of changes to all threads but does not ensure atomicity. synchronized guarantees both visibility and atomicity (by locking the monitor).
// Example of using volatile\npublic class VolatileExample {\n private volatile boolean running = true;\n\n public void stop() {\n running = false; // Change is immediately visible to other threads\n }\n\n public void run() {\n while (running) {\n // performing work\n }\n }\n}\n\nQuestion: What is happens-before in JMM?
\nAnswer: This is a partial ordering relationship that guarantees that actions in one thread will be visible to another. For example, a write to a volatile variable creates a happens-before relationship with a subsequent read of that variable by another thread. Also, happens-before occurs upon entering/exiting a synchronized block.
Question: How is ConcurrentHashMap structured?
\nAnswer: In Java 8+, ConcurrentHashMap uses segmentation at the bucket level. Instead of locking the entire table, only a specific bucket is locked (during collisions). Read operations do not use locks — they use volatile reads. Under high contention, internal structures can be restructured (e.g., a tree instead of a list).
// Example of ConcurrentHashMap\nConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();\nmap.put("key", 1);\nInteger value = map.get("key"); // No read lock\n\n2. Garbage Collection and GC Optimization
\nQuestions about the Garbage Collector (GC) are mandatory for Senior positions. You need to know not only the types of GC but also how to tune them under load.
\n\nQuestion: What are the main types of GC in the HotSpot JVM?
\nAnswer: The main collectors are:
\n- \n
- Serial GC — single-threaded, for client applications. \n
- Parallel GC (Throughput) — multi-threaded, focused on throughput. \n
- G1 GC (Garbage First) — default since Java 9, divides the heap into regions, minimizes pauses. \n
- ZGC and Shenandoah — low-latency collectors with pauses under 10 ms. \n
Question: What is a Stop-The-World pause?
\nAnswer: This is the moment when all application threads are paused so that the GC can perform its work. Long STW pauses are critical for real-time systems. G1 GC tries to minimize them by performing collection in parallel and incrementally.
\n\nQuestion: How to detect a memory leak in Java?
\nAnswer: Use profilers (VisualVM, JProfiler). Monitor the growth of heap dump. Typical causes of leaks: unreleased static collections, incorrect implementation of equals/hashCode in HashMap, listeners that are not unsubscribed.
// Example of a leak: static collection\npublic class LeakExample {\n private static List<byte[]> cache = new ArrayList<>();\n\n public void addData() {\n cache.add(new