Skip to content

Instantly share code, notes, and snippets.

@lreverchuk
Last active June 13, 2026 10:58
Show Gist options
  • Select an option

  • Save lreverchuk/b716440f68bba8577d48b43333fbda0d to your computer and use it in GitHub Desktop.

Select an option

Save lreverchuk/b716440f68bba8577d48b43333fbda0d to your computer and use it in GitHub Desktop.
Java Interview Questions and Answers: Core Concepts Explained

Java Interview Questions: Core Concepts With Answers

A focused set of Java interview questions with concise, correct answers, covering OOP, collections, memory and the JVM, concurrency, and common gotchas. Useful for interview prep or screening candidates.

OOP & language

What are the four pillars of OOP? Encapsulation (hide state behind methods), Inheritance (reuse via extends), Polymorphism (one interface, many implementations), Abstraction (expose what, hide how).

== vs .equals()? == compares references (same object); .equals() compares logical equality (override it for value objects). Always override hashCode() alongside equals().

String, StringBuilder, StringBuffer? String is immutable. StringBuilder is mutable and fast (not thread-safe). StringBuffer is mutable and synchronized (thread-safe, slower).

Why are Strings immutable? Security, thread safety, and the string pool (interning) for memory efficiency.

Interface vs abstract class? An interface declares a contract (since Java 8 it can have default methods); a class can implement many. An abstract class can hold state and constructors; a class extends only one.

What is method overloading vs overriding? Overloading = same name, different parameters (compile-time). Overriding = subclass redefines a parent method (runtime polymorphism).

final, finally, finalize? final = constant/non-overridable/non-extendable. finally = always-run block after try/catch. finalize() = deprecated GC hook (don't use it).

Collections

ArrayList vs LinkedList? ArrayList = backed by an array, fast random access (O(1)), slow inserts in the middle. LinkedList = doubly linked, fast inserts/removals at ends, slow random access.

HashMap vs HashTable vs ConcurrentHashMap? HashMap = unsynchronized, allows one null key. HashTable = legacy, fully synchronized. ConcurrentHashMap = thread-safe with fine-grained locking, the modern concurrent choice.

How does a HashMap work internally? Keys are hashed into buckets. Collisions chain in a linked list, converting to a balanced tree when a bucket grows large (Java 8+) for O(log n) worst case.

HashSet vs TreeSet? HashSet = unordered, O(1) operations. TreeSet = sorted, O(log n), backed by a red-black tree.

What is the difference between Comparable and Comparator? Comparable defines natural ordering inside the class (compareTo). Comparator is an external strategy (compare) for custom/multiple orderings.

JVM & memory

Explain JDK, JRE, and JVM. JVM runs bytecode. JRE = JVM + libraries (to run apps). JDK = JRE + compiler/tools (to build apps).

What are the memory areas? Heap (objects), Stack (per-thread frames/locals), Metaspace (class metadata), plus the program counter and native method stacks.

Stack vs heap? Stack holds method frames and primitives/references, freed automatically on return. Heap holds objects, managed by the garbage collector.

How does garbage collection work? The GC reclaims unreachable objects, typically using generational collection (young/old generations). You can't force it; System.gc() is only a suggestion.

What is a memory leak in Java? Objects no longer needed but still referenced (e.g. static collections, unclosed resources, listeners) so GC can't reclaim them.

Concurrency

Thread vs Runnable? Implement Runnable (or Callable) and pass it to a thread/executor, preferred over extending Thread because Java has single inheritance.

What is synchronized? A lock ensuring only one thread executes a block/method on a given monitor at a time, preventing race conditions.

volatile, what does it guarantee? Visibility: reads/writes go straight to main memory so threads see the latest value. It does not provide atomicity for compound operations.

What is the ExecutorService? A thread-pool abstraction for submitting tasks (submit, invokeAll) instead of manually managing threads.

What is a deadlock? Two+ threads each waiting on a lock the other holds. Avoid with consistent lock ordering or timeouts.

Common gotchas

Autoboxing pitfalls: comparing boxed Integer with == compares references; use .equals() or unbox. Integer cache: Integer values −128..127 are cached, so == may coincidentally work in that range, don't rely on it. Checked vs unchecked exceptions: checked must be declared/caught; unchecked (RuntimeException) need not be.

Quick checklist for candidates

Topic Can they explain…
OOP equals/hashCode contract
Collections ArrayList vs LinkedList, HashMap internals
JVM heap/stack, GC, JDK/JRE/JVM
Strings immutability + StringBuilder
Concurrency synchronized, volatile, ExecutorService
Exceptions checked vs unchecked

Maintained by the team at EchoGlobal. Hiring Java talent? See our curated lists of Top Java Experts, Top Spring Boot Experts, and Top Kotlin Developers on GitHub, or hire pre-vetted developers in days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment