Java fundamentals
New to Java interviews, or coming back to them. The questions almost every Java interview opens with: equality, strings, collections, exceptions, generics, then threads and memory.
16 questions, about 1 and a half hours of reading
Start with the first question- Explain the difference between == and equals for objects. What does System.identityHashCode return, and when would you use IdentityHashMap?
- State the equals contract and the hashCode contract. What breaks in a HashMap if a class overrides equals but not hashCode?
- Why is String immutable in Java? Give at least four concrete benefits and one cost.
- List the rules for writing an immutable class in Java, and explain why each rule matters.
- Compare abstract classes and interfaces in modern Java (Java 8 and later). When would you choose an abstract class?
- What is the difference between checked and unchecked exceptions? Give an example of each from the JDK and explain when to use which.
- What is type erasure? Give three things you cannot do because of it.
- How does ArrayList store elements and grow? What is the time complexity of add at the end, add at index 0, get, and remove from the middle?
- Walk through what java.util.HashMap does, from calling hashCode() to choosing a bucket, when you call map.put(key, value) in Java 8 or later.
- What is a lambda expression in Java, and what determines its type? Explain target typing with an example where the same lambda has two different types.
- What does it mean that streams are lazy? What does this print?
- Explain the difference between concurrency and parallelism, with one Java example of each. Can a program be concurrent on a single CPU core, and can parallel streams speed up an I/O-bound task?
- What lock does each of these use, and what guarantees does synchronized give beyond mutual exclusion? (1) a synchronized instance method, (2) a
- How does ConcurrentHashMap in Java 8 and later make put and get thread-safe without one global lock?
- What is stored on a Java thread stack, and what causes StackOverflowError compared with OutOfMemoryError: unable to create native thread?
- What are GC roots in the JVM? Name several kinds and explain how reachability decides what is garbage.
Mark a question as done on its page and it is ticked here. Kept in this browser only.