Scrivio Interview library

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
  1. Explain the difference between == and equals for objects. What does System.identityHashCode return, and when would you use IdentityHashMap?Object methods and contracts7 min
  2. State the equals contract and the hashCode contract. What breaks in a HashMap if a class overrides equals but not hashCode?Object methods and contracts6 min
  3. Why is String immutable in Java? Give at least four concrete benefits and one cost.Strings and text7 min
  4. List the rules for writing an immutable class in Java, and explain why each rule matters.Immutability and defensive copying7 min
  5. Compare abstract classes and interfaces in modern Java (Java 8 and later). When would you choose an abstract class?Interfaces and abstract classes6 min
  6. What is the difference between checked and unchecked exceptions? Give an example of each from the JDK and explain when to use which.Exceptions and error handling5 min
  7. What is type erasure? Give three things you cannot do because of it.Generics7 min
  8. 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?Lists6 min
  9. 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.HashMap and map implementations7 min
  10. 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.Lambdas and method references7 min
  11. What does it mean that streams are lazy? What does this print?Streams API5 min
  12. 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?Threads6 min
  13. What lock does each of these use, and what guarantees does synchronized give beyond mutual exclusion? (1) a synchronized instance method, (2) aSynchronization and monitors6 min
  14. How does ConcurrentHashMap in Java 8 and later make put and get thread-safe without one global lock?Concurrent collections8 min
  15. What is stored on a Java thread stack, and what causes StackOverflowError compared with OutOfMemoryError: unable to create native thread?Memory areas and object layout6 min
  16. What are GC roots in the JVM? Name several kinds and explain how reachability decides what is garbage.Garbage collection6 min

Mark a question as done on its page and it is ticked here. Kept in this browser only.