Java Interview Questions and Answers

These are core Java topics that come up again and again in technical interviews, each with a short answer you can say out loud. Use them to check your fundamentals, then practise the weak spots with quizzes and coding exercises.

Preparing for a senior role? See the guide for experienced professionals. For concurrency, jump to the multithreading page.

Core Java fundamentals

What is the difference between JDK, JRE and JVM?

The JVM executes bytecode. The JRE bundled the JVM with the standard class libraries needed to run programs. The JDK adds development tools such as the compiler (javac), jar and the debugger. Modern distributions ship the JDK; a separate JRE is no longer provided by default.

What is the difference between == and equals()?

== compares primitive values, or whether two references point to the same object. equals() compares logical equality and is defined by the class. If you override equals(), override hashCode() too: equal objects must return the same hash code, or hash-based collections will misbehave.

Why is String immutable and what is the string pool?

String objects cannot change after creation. That makes them safe to share between threads, safe as HashMap keys (the hash can be cached) and safe for security-sensitive values like class names. String literals are interned in a pool, so identical literals can share one instance. Use StringBuilder for repeated concatenation in loops.

What is the difference between an abstract class and an interface?

A class can implement many interfaces but extend only one class. Abstract classes can hold instance state and constructors. Interfaces describe a contract and, since Java 8, can include default and static methods. Prefer interfaces for capabilities and abstract classes when subclasses share state or partial implementation.

Collections

How does HashMap work internally?

A key's hashCode() is spread and mapped to a bucket in an array. Entries in the same bucket form a linked list, which is converted to a balanced tree when a bucket grows large (and the table is big enough), so worst-case lookups stay O(log n). The table resizes when the number of entries exceeds capacity times the load factor (0.75 by default). HashMap is not thread-safe.

ArrayList vs LinkedList: which should you use?

ArrayList is backed by an array: O(1) random access, amortised O(1) append, O(n) insert or remove in the middle. LinkedList gives O(1) insert/remove once you hold the node but O(n) to find it, and uses more memory per element. In practice ArrayList is the right default.

HashMap vs ConcurrentHashMap vs Hashtable?

HashMap is unsynchronised and allows one null key. Hashtable synchronises every method and is considered legacy. ConcurrentHashMap supports high-concurrency reads and fine-grained updates, offers atomic operations such as computeIfAbsent and merge, and does not allow null keys or values.

Exceptions, memory and streams

Checked vs unchecked exceptions?

Checked exceptions (subclasses of Exception other than RuntimeException) must be caught or declared. Unchecked exceptions (RuntimeException and Error) need not be. Use checked exceptions for recoverable conditions the caller is expected to handle, and unchecked ones for programming errors.

What are the main JVM memory areas?

The heap stores objects and is managed by the garbage collector. Each thread has its own stack of frames holding local variables and call state. Metaspace (native memory) holds class metadata. A StackOverflowError means a thread stack is exhausted; an OutOfMemoryError means the heap, metaspace or another memory area is.

What is the difference between map() and flatMap() in streams?

map() transforms each element into exactly one element. flatMap() transforms each element into a stream and flattens the results into one stream, for example turning a list of orders into a stream of all their line items.

List<String> items = orders.stream()
    .flatMap(order -> order.getItems().stream())
    .map(Item::getName)
    .toList();

Practice this on Quick Tech Learners

Reading answers is only the first step. Quick Tech Learners lets you turn these Java concepts into active practice once you create an account and sign in.

  • Interview Insights: browse interview questions shared by the community and filter them by topic tag.
  • Practice Quiz: choose the concepts you want to revise, answer questions, and review how you did.
  • Coding Practice: write and run code in a browser editor against test cases. Pick a language, a difficulty band from Beginner to Expert, and either Practice mode (with hints) or Interview mode.
  • Study Plan and reports: set the topics you are preparing, then check performance, mastery and readiness reports to see where the gaps are.
  • Share Interview Question: add a question you were asked so other learners can practise it.