Curated by Deen Bandhu — working Java developer & educator. These are questions actually asked at TCS, Infosys, Wipro, Capgemini, Cognizant, and product startups. Covers fresher to 1 years experience.
== and .equals()?▼== compares references (same object in heap?). .equals() compares content. For String always use .equals(). Example: new String("abc") == new String("abc") is false; .equals() is true.String s = "hello" checks the pool first — reuses existing "hello". new String("hello") always creates a new heap object bypassing the pool. Use .intern() to manually add to the pool.final, finally, and finalize()?▼final — keyword: constant variable, prevent overriding, prevent inheritance. finally — block always executed after try-catch (cleanup). finalize() — GC calls before destroying object (deprecated Java 9+, avoid it).int → Integer. Unboxing — reverse. Happens automatically when you add an int to an ArrayList<Integer>. Beware: unboxing a null Integer throws NullPointerException.extends. Polymorphism — overloading (compile-time) and overriding (runtime). Abstraction — hide implementation via abstract class or interface.default and static methods allowed. Use interface for capability (Runnable, Comparable). Use abstract class for shared base (same family of objects).@Override annotation — compiler will catch mistakes. You cannot override static, final, or private methods.Dog extends Animal). Composition = "has-a" (Car has an Engine). Prefer composition — more flexible, doesn't expose parent internals, easier to test. Inheritance creates tight coupling and can break when parent changes.private static class Holder { static final Singleton INSTANCE = new Singleton(); }. Alternatively use enum — simplest and safest Singleton in Java.put(k,v): compute k.hashCode(), spread hash, bucket index = hash & (n-1). Collision → linked list in bucket (red-black tree from Java 8 when list > 8). Default capacity: 16, load factor: 0.75. Resizes (doubles) when 75% full. Always override both hashCode() and equals() for custom keys.Collections.synchronizedMap() locks the entire map for every read/write — single lock = poor throughput under concurrency. ConcurrentHashMap uses per-bucket locking (Java 8: CAS + synchronized on individual buckets) — allows concurrent reads and multiple writes. Always prefer ConcurrentHashMap in multi-threaded code.Iterator: forward-only traversal, available for all Collections. Methods: hasNext(), next(), remove(). ListIterator: bidirectional (forward + backward), only for Lists. Additional methods: hasPrevious(), previous(), add(), set(). Always use Iterator to safely remove during iteration (avoid ConcurrentModificationException).Runnable. Extending Thread wastes your one inheritance slot. Runnable separates task from thread mechanism. Best: use ExecutorService with Runnable or Callable (returns result via Future) — avoids creating raw threads and enables thread reuse.synchronized?▼this. On static method: locks the Class object. On block: locks specified object. Prevents race conditions but hurts throughput under contention. Prefer ReentrantLock or concurrent collections where possible.volatile?▼AtomicBoolean / synchronized.newFixedThreadPool(n), newCachedThreadPool(), newSingleThreadExecutor(). Use submit(Callable) → returns Future. Always call shutdown() when done. Prefer over raw new Thread().throws or wrap in try-catch. Examples: IOException, SQLException. Unchecked: extend RuntimeException. Compiler doesn't enforce. Examples: NullPointerException, ArrayIndexOutOfBoundsException. Modern practice: prefer unchecked for programming errors, checked for recoverable situations.AutoCloseable resource (streams, connections, files) after the try block — even if exception is thrown. Cleaner than finally. Example: try (Connection c = ds.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) { ... }. Multiple resources closed in reverse declaration order.catch (IOException | SQLException e). The exception variable is implicitly final. Avoids code duplication when handling multiple exception types the same way. Can't catch exceptions that have an inheritance relationship in the same multi-catch block.(params) -> body. Enable functional programming style. Example: list.sort((a, b) -> a.compareTo(b)) or method reference: list.forEach(System.out::println). Four built-in functional interfaces: Predicate, Function, Consumer, Supplier.filter(), map(), flatMap(), sorted(), distinct(), limit(). Terminal (eager, triggers execution): collect(), forEach(), count(), reduce(), findFirst(). Streams don't mutate source. Use parallelStream() for CPU-bound operations on large data.null returns. Optional.of(val), Optional.ofNullable(val), Optional.empty(). Key methods: isPresent(), orElse(default), orElseThrow(), map(fn), ifPresent(consumer). Never call get() without isPresent() — defeats the purpose.default methods with a body — lets you add new methods without breaking all implementing classes. Also allows static methods in interfaces. Diamond problem: if two interfaces have same default method, the implementing class must override it to resolve conflict.System.gc() is just a hint — JVM may ignore it.StackOverflowError). Heap: shared across all threads, stores all objects. GC-managed. All new allocations go here. Larger but slower. OutOfMemoryError when heap is full.private final.These questions are from Deen Bandhu's Java course — students learn to answer all of these, build real projects, and crack interviews at product companies.