Java Core Interview Questions and Answers
Java for QA automation and developers: the JVM, types and strings, OOP, interfaces and records, collections and generics, exceptions, lambdas and streams, Optional, concurrency and the memory model, virtual threads, GC and memory, JUnit 5, TestNG, Mockito and AssertJ, Maven and Gradle, and parallel test frameworks.
225 questions for Java Core interviews with answers and explanations - Junior, Middle and Senior levels. Sample questions, online quizzes and coding tasks.
- OOP
- Collections
- Streams
- Concurrency
- JUnit and TestNG
Questions by level
Junior
Core concepts and syntax · 50 questions
Middle
Practice, nuances and optimization · 50 questions
Senior
Architecture, internals, edge cases · 50 questions
Sample questions
A few questions from each level with the correct answer and an explanation.
Java Core · Junior
- What does the JVM do?
Show the answer
Correct answer: Runs compiled bytecode on a specific platform
javac compiles .java files into .class bytecode. The JVM loads and runs that bytecode, interpreting it and compiling hot parts with the JIT.
- What's the difference between the JDK and the JRE?
Show the answer
Correct answer: The JDK adds development tools like javac to what is needed to run Java
The JRE was just what you needed to run programs. The JDK includes it plus the compiler, debugger and other tools. Modern Java ships the JDK only.
- Which of these is a primitive type in Java?
Show the answer
Correct answer:
intJava has eight primitives: byte, short, int, long, float, double, char, boolean. Integer is a wrapper class, String is a class.
- What is the default value of an
intfield in a class?Show the answer
Correct answer: 0
Fields get defaults: 0 for numbers, false for boolean, null for references. Local variables have no default and must be assigned before use.
Java Core · Middle
- How does
HashMapfind a value by key?Show the answer
Correct answer: It uses hashCode to pick a bucket, then equals to find the key in it
A good hashCode spreads keys across buckets. Long chains (since Java 8) turn into trees, but bad hash codes still slow things down.
- You put an object into a
HashSet, then change a field used in itshashCode. What happens?Show the answer
Correct answer: The set may no longer find it, though it is still inside
The object stays in the old bucket. Keys of hash collections should be immutable, at least in the fields used by equals and hashCode.
- What is the 'load factor' of a
HashMap?Show the answer
Correct answer: How full it may get before it resizes, 0.75 by default
When size exceeds capacity x load factor, the map doubles and rehashes. Setting the initial capacity avoids repeated resizing.
- What does PECS mean for generic wildcards?
Show the answer
Correct answer: Producer extends, consumer super
Read from a List<? extends Number>, write into a List<? super Integer>. Collections.copy uses exactly this.
Java Core · Senior
- What does 'happens-before' mean in the Java Memory Model?
Show the answer
Correct answer: If A happens-before B, B is guaranteed to see A's effects
Unlock-then-lock, volatile write-then-read, thread start and join create such edges. Without one, reads may see stale values.
- Why are properly built immutable objects safe to share between threads?
Show the answer
Correct answer: Final fields are guaranteed visible after construction, and nothing changes
The final-field semantics guarantee safe publication if
thisdoesn't escape the constructor. - Which thread runs
CompletableFuture.supplyAsync(task)without an executor?Show the answer
Correct answer: A thread from the shared ForkJoinPool.commonPool
Blocking I/O in the common pool starves parallel streams and other async code. Pass a dedicated executor for I/O.
- An exception is thrown inside
thenApply. What does the caller see onjoin()?Show the answer
Correct answer: A CompletionException wrapping the original cause
Handle with exceptionally or handle, and unwrap getCause() in tests to assert on the real exception.
Coding tasks
Write your solution right in the browser, and tests check it instantly. JavaScript interview classics (debounce, deepClone, Promise.all, an LRU cache), TypeScript tasks on generics, narrowing and utility types checked by the real compiler, Node.js backend logic (middleware, rate limiters, job queues), SQL queries in real PostgreSQL, and React components and hooks like a todo list, tabs, useDebounce and useInterval.
Open tasks →