Skip to content
GuideSphereCareers

Java · All levels

Java Interview Preparation: OOP, Collections, Exceptions and Concurrency

A focused revision guide to the Java questions most asked of freshers and early-career developers, with concise answers and examples.

Published 4 Dec 20263 min read

Java interviews typically move from OOP fundamentals to collections, exceptions, memory and multithreading. Revise each section, then write small programs to prove you understand it.

JDK, JRE and JVM

  • JVM (Java Virtual Machine) runs compiled bytecode and manages memory.
  • JRE is the runtime environment: the JVM plus core libraries.
  • JDK is the development kit: the JRE plus tools such as the compiler (javac).

Java source compiles to platform-independent bytecode, which is why Java is described as "write once, run anywhere".

The four pillars of OOP

Pillar Meaning Java example
Encapsulation Hide state, expose behaviour private fields + getters/setters
Inheritance Reuse and extend a class class Car extends Vehicle
Polymorphism One interface, many forms Method overriding, overloading
Abstraction Expose what, hide how Abstract classes, interfaces

Q: Overloading vs overriding? Overloading = same method name, different parameters, resolved at compile time. Overriding = a subclass redefines an inherited method with the same signature, resolved at runtime.

Q: Abstract class vs interface? A class can extend only one abstract class but implement many interfaces. Abstract classes can hold state and constructors; interfaces define a contract and (since Java 8) may contain default and static methods.

Strings

Q: Why are Strings immutable? Immutability makes strings safe to share, lets the JVM reuse literals in the string pool, and makes them safe as HashMap keys.

Q: String vs StringBuilder vs StringBuffer? StringBuilder is mutable and fast for building strings in a loop; StringBuffer is its synchronised (thread-safe, slower) counterpart.

Q: == vs equals()? == compares references; equals() compares content (when overridden, as in String).

equals() and hashCode()

If two objects are equal according to equals(), they must return the same hashCode(). Breaking this contract makes HashMap and HashSet behave incorrectly. Always override both together.

Collections framework

Interface Common implementations Notes
List ArrayList, LinkedList Ordered, allows duplicates
Set HashSet, LinkedHashSet, TreeSet No duplicates; TreeSet is sorted
Map HashMap, LinkedHashMap, TreeMap Key → value
Queue/Deque ArrayDeque, PriorityQueue FIFO / priority ordering

Q: ArrayList vs LinkedList? ArrayList gives O(1) random access and is usually faster in practice; LinkedList allows cheap insertion/removal at known nodes but O(n) access by index.

Q: How does HashMap work? Keys are hashed into buckets. Collisions are stored in a list within the bucket; since Java 8, a heavily-collided bucket is converted into a balanced tree. Average get/put is O(1).

Q: HashMap vs ConcurrentHashMap? HashMap is not thread-safe. ConcurrentHashMap supports safe concurrent access without locking the whole map.

Exceptions

  • Checked exceptions (e.g. IOException) must be declared or handled at compile time.
  • Unchecked exceptions (RuntimeException subclasses such as NullPointerException) need not be declared.
  • Error (e.g. OutOfMemoryError) signals serious problems you normally should not catch.

Use try-with-resources to close resources automatically:

try (BufferedReader br = Files.newBufferedReader(Path.of("data.txt"))) {
    return br.readLine();
}

Memory and garbage collection

Objects live on the heap; method calls and local primitives/references live on the stack. The garbage collector frees objects that are no longer reachable. You cannot force collection; System.gc() is only a request.

Multithreading basics

Q: How do you create a thread? Implement Runnable (or Callable) and run it on a Thread or, preferably, an ExecutorService.

Q: What does synchronized do? It allows only one thread at a time into the guarded block or method for a given lock, preventing race conditions.

Q: What does volatile guarantee? Visibility: writes by one thread are seen by others. It does not make compound actions like count++ atomic — use AtomicInteger or locks for that.

Q: What is a deadlock? Two or more threads each wait forever for a lock held by another. Avoid it by acquiring locks in a consistent order and using timeouts.

Java 8+ features worth knowing

  • Lambdas and functional interfaces (Predicate, Function, Supplier).
  • Streams: list.stream().filter(...).map(...).collect(Collectors.toList()).
  • Optional to represent "maybe a value" instead of returning null.
  • Records (Java 16+) for concise immutable data classes.

Practice checklist

  • Implement a class with correct equals() and hashCode() and use it as a HashMap key.
  • Count word frequency using a HashMap and then using Streams.
  • Write a producer–consumer example with BlockingQueue.
  • Explain the output of string comparison questions using == and equals().
  • #java
  • #oop
  • #freshers