Java Interview Questions and Answers

JVM fundamentals, OOP, collections, concurrency and Spring concepts.

Practise 10 random 3 peer-reviewed questions
Java Interview Syllabus & Preparation Strategy

Whether you are preparing for entry-level Java interview questions for freshers or senior software engineer interview questions addressing concurrency, scalability, and system architecture, this track provides peer-reviewed model answers with syntax walkthroughs, edge cases, and practical interview tips.

1 Compare ArrayList, LinkedList and HashMap. Medium
  • ArrayList: array-backed, O(1) random access, amortised O(1) append, O(n) insertion/removal in the middle. Best default for ordered lists.
  • LinkedList: doubly linked, O(1) insertion/removal at the ends or given a node, O(n) random access and poor cache locality. Rarely the best choice in practice; ArrayDeque is preferred for queues.
  • HashMap: hash table with average O(1) get/put, no ordering guarantee, one null key allowed. Collisions are handled with buckets that treeify to red-black trees past a threshold. Use LinkedHashMap for insertion order and TreeMap for sorted keys.

Other essentials: HashSet is a HashMap key set; ConcurrentHashMap for thread-safe maps; choose initial capacity and load factor to avoid rehashing.

2 Explain equals and hashCode contract. Medium

Rules:

  • If a.equals(b) is true then a.hashCode() == b.hashCode() must be true.
  • If two objects are unequal, their hash codes may still collide (allowed).
  • equals must be reflexive, symmetric, transitive and consistent.
  • Override both together, or hash-based collections (HashMap, HashSet) misbehave.
@Override
public boolean equals(Object o) {
  if (this == o) return true;
  if (!(o instanceof User u)) return false;   // pattern matching (Java 16+)
  return id == u.id && Objects.equals(email, u.email);
}

@Override
public int hashCode() { return Objects.hash(id, email); }

Mutable fields used in equals/hashCode are a classic bug: mutating them after insertion corrupts the collection.

3 What is Spring dependency injection and why use it? Medium

Dependency injection means a component receives its collaborators from outside rather than creating them itself. The Spring container builds and wires the object graph from annotations such as @Component, @Service and @Repository, injecting through constructor, setter or field.

@Service
public class OrderService {
  private final PaymentGateway gateway;
  public OrderService(PaymentGateway gateway) { this.gateway = gateway; }
}

Benefits: loose coupling, easier testing with mocks, centralised configuration and lifecycle management. Constructor injection is preferred because dependencies are explicit, objects can be immutable and missing beans fail fast at startup. Spring Boot adds auto-configuration and a starter-based dependency model.

Frequently Asked Questions About Java Interviews

What do hiring managers evaluate in Java technical rounds?

Technical interviewers look for foundational fluency, idiomatic syntax, clarity when communicating complex logic, and awareness of performance trade-offs (e.g. memory footprint, render performance, and network latency) in production environments.

What are the best interview tips for practicing Java questions?

Use active recall: summarize each answer in your own words before revealing the model solution. Focus on explaining why a certain approach is chosen rather than just memorizing code syntax.