D
DevPrepSystematic Prep
JAVA · MEDIUM CODEX

Java Interview Questions — Medium

Real-world mechanisms, state management, edge cases, performance trade-offs, and practical coding.

50 Theory Questions7 Implementation Folios5 Free Model Answers
THEORY QUESTIONS & SOLUTIONSShowing 5 of 50 questions
Q1What is the difference between fail-fast and fail-safe iterators?
  • Fail-Fast:
    • Mechanism: Operates directly on the collection's structure. If the collection is structurally modified (added/removed) after the iterator is created, it immediately throws ConcurrentModificationException using a modCount check.
    • Examples: ArrayList, HashMap, HashSet iterators.
  • Fail-Safe (Weakly Consistent):
    • Mechanism: Operates on a clone or snapshot of the collection, or utilizes lock-free concurrent structures. It does not throw exceptions if modifications occur during iteration.
    • Examples: CopyOnWriteArrayList, ConcurrentHashMap iterators.
Q2Explain how ConcurrentHashMap achieves high concurrency in Java 8.
  • Locking Mechanism: Instead of locking the entire map (like Hashtable or Collections.synchronizedMap), it uses Node-level synchronization using CAS (Compare-And-Swap) for empty buckets and locks only the head node of a specific bucket segment during write operations.
  • Concurrency Level: Allows concurrent read operations without locking and locks individual buckets during writes, enabling high throughput under heavy multithreaded loads.
Q3Explain the difference between ExecutorService.submit() and execute().
  • execute(): Defined in the Executor interface. Accepts a Runnable task, returns void (fire-and-forget), and propagates unhandled exceptions directly to the thread's uncaught exception handler.
  • submit(): Defined in ExecutorService. Accepts Runnable or Callable tasks, returns a Future object wrapping the execution state, and swallows exceptions, returning them inside the Future.get() call.
Q4Detail the parameters of ThreadPoolExecutor.
  • corePoolSize: The minimum number of threads kept alive in the pool, even if they are idle.
  • maximumPoolSize: The maximum number of active threads allowed to run concurrently in the pool.
  • keepAliveTime: The maximum duration that excess idle threads (beyond core size) wait for new tasks before terminating.
  • workQueue: The queue (BlockingQueue) holding tasks before they are picked up by active threads (e.g., LinkedBlockingQueue, SynchronousQueue).
Q5What are the different types of thread pools created by the Executors utility?
  • Fixed Thread Pool (newFixedThreadPool(n)): Uses a fixed number of threads and an unbounded queue.
  • Cached Thread Pool (newCachedThreadPool()): Spawns new threads dynamically as needed, reclaiming idle threads after 60 seconds; uses a SynchronousQueue (no storage capacity).
  • Single Thread Executor (newSingleThreadExecutor()): Spawns exactly one worker thread executing tasks sequentially in FIFO order.
  • Scheduled Thread Pool (newScheduledThreadPool(n)): Scheduled to execute commands after a delay or periodically.

Unlock the remaining 52 Java (Medium) questions

You've completed the 5 free sample questions. Get unrestricted lifetime access to every question, model answer, implementation challenge, and all 27+ technologies for a single payment.

₹399 India / $9 International · One-time settlement · Zero subscription

CROSS-DIFFICULTY NAVIGATION

Continue Preparing Java

Java Interview Questions (Medium) | DevPrep | DevPrep