What this round is for
Java rounds are predictable in a useful way. Expect collections and the equals-hashCode contract, then concurrency and the memory model, then generics and erasure - and somewhere a question designed to find out whether you learned the language from material written a decade ago.
Several of the answers below concern failures that are silent rather than loud: a broken hash contract, a mutated key, a leak held by a static map. Those are what distinguish candidates with production experience.
The questions
Answer each one out loud before you open it. An answer you have read is not an answer you can
give under pressure, which is the only kind that counts.
Q1
What is the JVM and what does it buy you?
Show the answer
Hide the answer
A virtual machine that runs bytecode, so the same compiled output runs anywhere a JVM exists.
It also optimises while running - inlining hot methods and compiling interpreted code to native instructions based on what the program actually does.
What it is really testing.
Whether you mention run-time optimisation rather than only portability.
Q2
Why is a Java program slow for the first few seconds?
Show the answer
Hide the answer
The JIT compiler has not yet identified and optimised the hot paths.
Benchmarking the first second measures the interpreter rather than the program, which is why warm-up matters in any Java measurement.
What it is really testing.
Whether you know to warm up before measuring.
Q3
What is the difference between JDK, JRE and JVM?
Show the answer
Hide the answer
The JVM runs bytecode, the JRE adds the standard library, the JDK adds the compiler and tools.
You need the JDK to build and historically only the JRE to run, though modern distributions have largely merged them.
What it is really testing.
Whether you can separate the three rather than using them interchangeably.
Q4
What is the difference between a primitive and a wrapper?
Show the answer
Hide the answer
A primitive holds a value directly; a wrapper is an object that can be null and used in collections.
Autoboxing hides the conversion, which is why an Integer compared with == can be false for equal values above the cache range.
What it is really testing.
Whether you know autoboxing exists and what it hides.
Q5
Why can comparing two Integers with == fail?
Show the answer
Hide the answer
== compares references, and only small values are cached as shared objects.
Values from -128 to 127 are cached so == appears to work, then stops at 128. Always use equals for wrapper comparison.
What it is really testing.
Whether you know about the integer cache. It is a classic trick question.
Q6
What is the difference between String, StringBuilder and StringBuffer?
Show the answer
Hide the answer
String is immutable, StringBuilder is mutable and fast, StringBuffer is the synchronised and slower variant.
Building a string in a loop with concatenation creates a new object each time, which is quadratic. StringBuilder is the fix.
What it is really testing.
Whether you know loop concatenation is quadratic.
Q7
Why are Strings immutable in Java?
Show the answer
Hide the answer
For safety in the string pool, for hashing, and so they can be shared across threads without locks.
Because they are cached in a pool, a mutable String could change a value another part of the program relies on.
What it is really testing.
Whether you can give more than one reason.
Q8
What is the contract between equals and hashCode?
Show the answer
Hide the answer
Equal objects must return the same hash code; unequal objects may collide.
Break it and an object stored in a HashMap becomes unfindable, with no error raised - the map still holds it but lookup goes to the wrong bucket.
What it is really testing.
Whether you can name the failure - a lost map entry with no exception.
Q9
What happens if you mutate a key after putting it in a HashMap?
Show the answer
Hide the answer
If the mutation changes the hash code, the entry becomes unreachable.
This is the practical argument for immutable keys, and one reason records are a useful addition to the language.
What it is really testing.
Whether you connect it to the argument for immutable keys.
Q10
What is the difference between an abstract class and an interface in Java?
Show the answer
Hide the answer
An abstract class may hold state and partial implementation; an interface declares a contract.
Java allows one superclass and many interfaces, which often decides it. Interfaces can now have default methods, which narrowed the gap.
What it is really testing.
Whether you know default methods narrowed the difference.
Q11
Why program against interfaces?
Show the answer
Hide the answer
So callers depend on a contract rather than an implementation, and the implementation can be substituted.
It is what makes code testable without a mocking framework - any object satisfying the interface can be passed in.
What it is really testing.
Whether you connect it to testability rather than treating it as theory.
Q12
What is dependency injection and do you need a framework?
Show the answer
Hide the answer
Passing dependencies in rather than constructing them - and no, a constructor parameter is enough.
A framework only automates the wiring. A class that builds its own database connection cannot be tested in isolation.
What it is really testing.
Whether you separate the pattern from the framework.
Q13
What are checked exceptions and why are they controversial?
Show the answer
Hide the answer
Failures the compiler forces you to catch or declare.
The intent was sound, but declarations propagate through layers that do not care, and people wrap everything in catch blocks that log and continue.
What it is really testing.
Whether you can argue both sides. No language since has copied it.
Q14
What is the difference between Error, Exception and RuntimeException?
Show the answer
Hide the answer
Error signals a serious JVM problem, Exception an anticipated failure, RuntimeException an unchecked programming error.
Catching Error is almost always wrong. Catching Throwable catches both and should be rare and deliberate.
What it is really testing.
Whether you know which ones you should not catch.
Q15
What does try-with-resources do?
Show the answer
Hide the answer
It closes resources automatically at the end of the block, even if it throws.
It replaced nested finally blocks and removes the possibility of forgetting, which was a steady source of connection leaks.
What it is really testing.
Whether you know what it replaced and why that mattered.
Q16
What is the difference between ArrayList and LinkedList?
Show the answer
Hide the answer
ArrayList has fast indexed access and cheap appends; LinkedList has cheap insertion once positioned.
LinkedList is almost never the right choice in practice, because chasing pointers defeats the CPU cache badly enough to lose even where theory favours it.
What it is really testing.
Whether you mention cache behaviour. The textbook answer alone is incomplete.
Q17
How does HashMap work internally?
Show the answer
Hide the answer
Keys are hashed to a bucket; collisions are chained, and long chains become a balanced tree.
The treeification was added so that adversarial hash collisions degrade to logarithmic rather than linear lookup.
What it is really testing.
Whether you know about treeification, which is a relatively recent change.
Q18
What is the difference between HashMap, LinkedHashMap and TreeMap?
Show the answer
Hide the answer
HashMap has no ordering, LinkedHashMap keeps insertion order, TreeMap keeps keys sorted.
TreeMap costs logarithmic time for the ordering. Choosing by what you need to iterate rather than by habit is the point.
What it is really testing.
Whether you choose by required ordering rather than by habit.
Q19
What is the difference between fail-fast and fail-safe iterators?
Show the answer
Hide the answer
Fail-fast throws if the collection is modified during iteration; fail-safe iterates a copy.
Most standard collections are fail-fast, which turns a subtle concurrency bug into an immediate exception.
What it is really testing.
Whether you know most standard collections are fail-fast.
Q20
What are generics and what is type erasure?
Show the answer
Hide the answer
Generics give collections a type at compile time; erasure removes the parameter before run time.
So you cannot create an array of a generic type or check instanceof against one. It was done for backwards compatibility.
What it is really testing.
Whether you know erasure explains the restrictions rather than them being arbitrary.
Q21
What is the difference between extends and super in a wildcard?
Show the answer
Hide the answer
extends means read-only producer, super means write-only consumer.
The mnemonic is producer-extends, consumer-super. A list of extends Number can be read as Number but not added to.
What it is really testing.
Whether you can state the producer-consumer mnemonic and apply it.
Q22
What is the difference between == and equals for objects?
Show the answer
Hide the answer
== compares references; equals compares value, if the class overrides it.
A class that does not override equals inherits reference comparison, which is why two equal-looking objects can be unequal.
What it is really testing.
Whether you know the default inherited behaviour.
Q23
What is the Java memory model concerned with?
Show the answer
Hide the answer
When one thread's writes become visible to another.
Without synchronisation or volatile there is no guarantee a change is ever seen, because of caching and reordering - not just interleaving.
What it is really testing.
Whether you mention visibility rather than only interleaving.
Q24
What does volatile do?
Show the answer
Hide the answer
It guarantees visibility of writes across threads and prevents certain reorderings.
It does not make compound operations atomic, so a volatile counter increment is still a race.
What it is really testing.
Whether you know it does not make increments atomic.
Q25
What is the difference between synchronized and a Lock?
Show the answer
Hide the answer
synchronized is simpler and scoped to a block; a Lock allows timeouts, interruption and multiple conditions.
Most code should use neither directly - the concurrent collections and executors in the standard library are usually the right level.
What it is really testing.
Whether you recommend the higher-level tools instead of either.
Q26
What is an ExecutorService for?
Show the answer
Hide the answer
Running tasks on a managed thread pool rather than creating threads by hand.
Creating a thread per task exhausts memory at scale, because each traditional thread reserves about a megabyte of stack.
What it is really testing.
Whether you know why thread-per-task does not scale.
Q27
What are virtual threads and what do they change?
Show the answer
Hide the answer
Lightweight threads managed by the JVM, cheap enough to create in millions.
They make the simple blocking style viable again at scale, which removes much of the motivation for reactive programming.
What it is really testing.
Whether you know they change the economics of blocking code.
Q28
What is the difference between a stream and a collection?
Show the answer
Hide the answer
A collection holds elements; a stream describes a computation over them and is consumed once.
Streams are lazy, so nothing happens until a terminal operation, and a stream cannot be reused after that.
What it is really testing.
Whether you know a stream is single-use.
Q29
When is a parallel stream a bad idea?
Show the answer
Hide the answer
For small collections, or where the work per element is tiny.
Coordination overhead frequently costs more than the parallelism saves, and the shared pool can be starved by a blocking operation.
What it is really testing.
Whether you can name a case where it is slower.
Q30
What is Optional for and how is it misused?
Show the answer
Hide the answer
Expressing that a return value may be absent - misused as a field or a parameter type.
As a field it adds a wrapper without adding information and breaks serialisation. Calling get without checking defeats the purpose entirely.
What it is really testing.
Whether you know the misuses. Advocating it everywhere is a weaker answer.
Q31
What is a record?
Show the answer
Hide the answer
A concise class for data that generates its constructor, accessors, equals, hashCode and toString.
It removes about sixty lines of boilerplate whose equals and hashCode were frequently written incorrectly.
What it is really testing.
Whether you know what it generates and why that matters.
Q32
What is a sealed interface for?
Show the answer
Hide the answer
Restricting which types may implement it, so the compiler knows the complete set.
Combined with pattern matching in switch it allows exhaustiveness checking, which is how you model a closed set of states.
What it is really testing.
Whether you connect it to exhaustive pattern matching.
Q33
What is the difference between final, finally and finalize?
Show the answer
Hide the answer
final prevents reassignment or overriding, finally always runs, finalize was a deprecated cleanup hook.
finalize is removed in practice - it had unpredictable timing and is replaced by try-with-resources and cleaners.
What it is really testing.
Whether you know finalize is effectively gone.
Q34
What does static mean and when is it a problem?
Show the answer
Hide the answer
It belongs to the class rather than an instance - and it is a problem when it holds state or reaches outside.
A static method that touches a database or a clock cannot be substituted in a test, which is the usual cause of untestable classes.
What it is really testing.
Whether you connect static to testability.
Q35
What is the difference between an inner class and a static nested class?
Show the answer
Hide the answer
An inner class holds a reference to its enclosing instance; a static nested class does not.
That hidden reference can keep a large object alive, which is a classic subtle memory leak.
What it is really testing.
Whether you know the hidden enclosing reference can leak memory.
Q36
How does garbage collection work at a high level?
Show the answer
Hide the answer
Unreachable objects are reclaimed; generational collectors assume most objects die young.
You rarely tune it directly, but knowing the generational assumption explains why allocating many short-lived objects is cheaper than it sounds.
What it is really testing.
Whether you know the generational assumption and what follows from it.
Q37
What causes a memory leak in a garbage-collected language?
Show the answer
Hide the answer
Holding references you no longer need - caches, listeners, static collections, ThreadLocals.
The collector only frees what is unreachable, so a forgotten entry in a static map is a leak the language cannot help with.
What it is really testing.
Whether you can name a concrete source rather than saying it cannot happen.
Q38
What is the difference between a shallow and a deep copy in Java?
Show the answer
Hide the answer
A shallow copy shares nested objects; a deep copy duplicates them.
Clone is shallow by default and its contract is awkward enough that copy constructors or factory methods are usually preferred.
What it is really testing.
Whether you know clone is shallow and awkwardly specified.
Q39
How do Maven and Gradle affect your security posture?
Show the answer
Hide the answer
They pull transitive dependencies, so your application contains libraries you never chose.
Running a dependency audit is a five-minute task most teams skip, and it is the highest-value security habit in the ecosystem.
What it is really testing.
Whether you have ever run an audit. Most candidates have not.
Q40
Where is Java a good fit and where is it not?
Show the answer
Hide the answer
Good for large, long-lived systems maintained by teams; poor for short scripts and start-up-sensitive workloads.
The verbosity people complain about is the price of the explicitness that makes year eight survivable. Native compilation has narrowed the start-up gap.
What it is really testing.
Whether you can name where it is a poor fit rather than advocating uncritically.