Interview Prep Guide

Java Interview Questions and Answers for Freshers to Experienced Developers

Prepare for Java interviews with clear questions on JVM basics, OOP, collections, exceptions, concurrency, and coding-round expectations.

Basic Java Interview Questions

  1. What is the difference between JDK, JRE, and JVM?

    JVM runs Java bytecode, JRE provides the runtime environment for running Java applications, and JDK includes the JRE plus developer tools like the compiler and debugger.

  2. What are the main OOP principles in Java?

    The main OOP principles are encapsulation, inheritance, polymorphism, and abstraction.

  3. What is the difference between == and equals() in Java?

    == compares object references for identity, while equals() compares logical equality when the class implements it appropriately.

  4. What are the accessibility modifiers in Java?

    Java has four main access levels: private (class only), default (package only), protected (package + subclasses), and public (everywhere).

Medium Java Interview Questions

  1. What is the difference between String, StringBuilder, and StringBuffer?

    String is immutable, StringBuilder is mutable and not synchronized, and StringBuffer is mutable but synchronized for thread safety.

  2. What is the difference between checked and unchecked exceptions?

    Checked exceptions must be handled or declared at compile time, while unchecked exceptions extend RuntimeException and are not enforced by the compiler.

  3. What is the difference between method overloading and overriding?

    Overloading means multiple methods share a name with different parameter lists, while overriding means a subclass provides a new implementation for an inherited method.

  4. How do List, Set, and Map differ in the Java Collections Framework?

    List is an ordered collection that allows duplicates, Set is an unordered collection that forbids duplicates, and Map is an association of unique keys to values.

  5. What is an interface and how is it different from an abstract class?

    An interface defines a contract for behavior that any class can implement, while an abstract class provides a partial implementation that subclasses share.

Advanced Java Interview Questions

  1. Why must equals() and hashCode() stay consistent?

    If two objects are equal according to equals(), they must return the same hashCode(), otherwise hash-based collections like HashMap and HashSet will behave incorrectly.

  2. How would you explain synchronized, volatile, and concurrent collections?

    synchronized controls mutual exclusion, volatile ensures visibility of variable updates across threads, and concurrent collections provide thread-safe data structures with better scalability than coarse locking in many cases.

  3. How do heap memory, stack memory, and garbage collection matter in Java?

    Heap memory stores objects, stack memory stores method-call frames and local references, and garbage collection reclaims heap objects that are no longer reachable.

  4. What are the main types of Garbage Collectors in Java?

    Java has several GCs such as Serial, Parallel, CMS (deprecated), G1 (default since Java 9), and ZGC (low-latency).

  5. What is the Java Stream API and how does it relate to lambdas?

    The Stream API allows for functional-style operations on sequences of elements, using lambdas to define transformations like filter, map, and collect.

Scenario-Based Java Interview Questions

  1. How would you reason about a Java service that is reliable in testing but fails under concurrent production traffic?

    Look for shared mutable state, transaction boundaries, connection usage, thread safety, and production-only data or traffic patterns.

Modern Java Production Questions

  1. Why must equals and hashCode be implemented consistently?

    Objects considered equal must return the same hash code so hash-based collections can place and retrieve them consistently.

  2. How do you design an immutable Java class?

    Make state private and final, prevent unsafe subclass mutation, validate construction, avoid setters, and defensively copy mutable inputs and outputs.

  3. How would you investigate high garbage-collection pause time?

    Inspect allocation rate, heap occupancy, pause logs, collector behavior, object lifetime, and heap dumps before changing heap size or collector flags.

  4. When should you use an ExecutorService instead of creating threads directly?

    ExecutorService centralizes bounded concurrency, queueing, lifecycle, cancellation, and rejection behavior so resource use can be controlled.

  5. How do you handle errors and cancellation with CompletableFuture?

    Compose stages deliberately, define exception recovery, propagate cancellation where meaningful, choose executors explicitly, and avoid blocking common-pool threads.

  6. What does transaction propagation mean in a Java service?

    Propagation defines whether a method joins, creates, suspends, or rejects a surrounding transaction, affecting atomicity, locking, and failure behavior.

  7. How would you diagnose a Java memory leak?

    Confirm sustained post-GC growth, capture comparable heap dumps, inspect dominator trees and retention paths, then fix the owner retaining objects unnecessarily.

Java Coding Round

  1. Design a simple LRU cache or frequency counter using collections

    The interviewer usually watches whether you can map requirements to the right collection strategy instead of writing random loops first. A good answer explains why a map is needed, how ordering or frequency is maintained, and where edge cases appear.

  2. Find the first non-repeating character in a string using Java 8+ Streams

    A modern Java solution uses chars() to get a stream, LinkedHashMap with Collectors.groupingBy and counting() to maintain order and frequency, and then filters for count 1. This shows you move beyond simple loops to functional patterns.

  3. Design and code a small service class with validation, exception handling, and clean object modeling

    Interviewers usually want to see whether your Java code is structured, testable, and explicit. Strong answers use clear models, avoid overengineering, and explain why each class or method has its current responsibility.