Java
What gets asked
The Java questions that come up in nearly every round.
More on this is coming, and asking moves it up the list.
Or sit with someone on it instead of waiting for us to write it.
Java
The Java questions that come up in nearly every round.
More on this is coming, and asking moves it up the list.
Or sit with someone on it instead of waiting for us to write it.
Want this written up?
Sign in and tell us. We write the ones people actually ask for, and asking takes one click.
Sign in to askThe Java interview is more predictable than almost any other part of the process. The same eight or nine things come up, and they come up because each one has a wrong answer that sounds right.
What follows is not a list to memorise. It is what each question is actually checking, which is the part that decides how you do.
"Difference between an interface and an abstract class." Since default methods arrived the mechanical differences shrank, so the honest answer is about intent: an interface is a capability something has, an abstract class is a partial implementation things share. If you only recite "a class can implement many interfaces", you have answered the 2010 version.
"What is the difference between == and .equals()?" Reference identity
against value equality. The follow-up is always about String, and the answer
involves the string pool: literals are interned and shared, new String("a") is
not, so == can be true for two literals and false for two objects holding the
same characters.
"Is Java pass by value or pass by reference?" Always pass by value. The subtlety is that when the value is a reference, you get a copy of the reference, so you can mutate the object it points at but reassigning the parameter does nothing outside the method. People who say "pass by reference for objects" are describing the right behaviour with the wrong words, and the interviewer is listening for exactly that.
"Why must you override hashCode when you override equals?" Because a
hash-based collection finds the bucket first and compares second. Get this one
right and it usually ends the topic.
"What does static actually mean?" It belongs to the class, not an instance.
The real follow-up is why a static method cannot use this, and whether static
state is safe across threads, which it is not.
"What is final?" Three separate meanings: a field that cannot be
reassigned, a method that cannot be overridden, a class that cannot be extended.
Note the first one does not make the object immutable, and
say so before they ask.
"Checked or unchecked exceptions?" Checked means the compiler forces you to handle or declare it. The interesting answer is your opinion on when checked exceptions earn their cost, because most modern code prefers unchecked.
"How does a HashMap work?" Buckets from the hash, equals inside the
bucket, resize when the load factor is passed, and since Java 8 a bucket that
gets too long becomes a tree rather than a list.
"What is volatile?" Visibility and ordering, not atomicity. If you can add
that volatile int count still loses increments, you have shown you understand
it rather than remembered it.
Almost none of this is knowledge retrieval. They already assume you can look things up.
They are checking whether you can be precise under mild pressure. Whether "I am not certain, here is how I would check" comes out instead of a confident wrong answer. Whether you notice the follow-up is a different question from the first one.
"Tell me about a bug you found that was hard." This is the real question in the whole interview. They want to hear a method: what you observed, what you suspected, how you narrowed it, what it turned out to be. A story with a hypothesis in it beats a story with a hero in it.
"Why did you do it that way?", asked about your own code. There is no preparation for this other than having actually made the decision. If an agent wrote it and you did not review it, this is exactly where that shows.
Write the answers out loud rather than reading them. The failure mode is almost never not knowing. It is knowing and not being able to say it in order.