Interview questions

400+ interview questions, answered

Curated from real hiring rounds at our partner companies. Every answer is written to be said out loud in under 30 seconds.

Beginner → Advanced

Java

Core language, OOP, collections, concurrency and JVM internals asked in service and product interviews.

  1. 1.What is the difference between JDK, JRE and JVM?

    JDK is the development kit (compiler + tools), JRE is the runtime with core libraries, and the JVM is the engine that actually executes bytecode.

  2. 2.Why is Java called platform independent?

    Source compiles to bytecode, and any platform with a JVM can run that same bytecode without recompilation.

  3. 3.Explain the four pillars of OOP.

    Encapsulation hides state behind methods, abstraction exposes only intent, inheritance reuses behaviour, and polymorphism lets one interface take many forms.

  4. 4.Difference between overloading and overriding?

    Overloading is compile-time, same method name with different parameters; overriding is runtime, a subclass replacing a parent implementation with the same signature.

  5. 5.Can you override a static method?

    No. Static methods belong to the class and are hidden, not overridden, when redeclared in a subclass.

  6. 6.What is the difference between == and equals()?

    == compares references (or primitive values); equals() compares logical content and is meant to be overridden.

  7. 7.Why must you override hashCode() when you override equals()?

    Hash-based collections locate objects by hash bucket first, so equal objects with different hashes will be lost in a HashMap or HashSet.

  8. 8.What is the String pool?

    A JVM-managed cache in heap where string literals are interned so identical literals share one object.

  9. 9.Why is String immutable?

    Immutability makes strings safe to share across threads, cacheable in the pool, and reliable as HashMap keys.

  10. 10.StringBuilder vs StringBuffer?

    Both are mutable; StringBuffer is synchronised and slower, StringBuilder is unsynchronised and preferred for single-threaded code.

  11. 11.What is the difference between ArrayList and LinkedList?

    ArrayList is a resizable array with O(1) random access; LinkedList is a doubly linked list with cheap inserts and removals at known nodes.

  12. 12.ArrayList vs Vector?

    Vector is synchronised and grows 100%, ArrayList is unsynchronised and grows 50% — use ArrayList with explicit locking or CopyOnWriteArrayList.

  13. 13.How does HashMap work internally?

    Keys are hashed into buckets; collisions form a linked list that converts to a red-black tree after eight entries, and the table resizes at the load factor of 0.75.

  14. 14.HashMap vs ConcurrentHashMap?

    ConcurrentHashMap allows concurrent reads and segment/bin-level locked writes without locking the whole map, and rejects null keys and values.

  15. 15.What is the contract of Comparable vs Comparator?

    Comparable defines the natural ordering inside the class via compareTo(); Comparator defines external, swappable orderings via compare().

  16. 16.Difference between HashSet, LinkedHashSet and TreeSet?

    HashSet is unordered, LinkedHashSet preserves insertion order, TreeSet keeps elements sorted with O(log n) operations.

  17. 17.What is autoboxing and why is it risky?

    Automatic conversion between primitives and wrappers; risky because it can throw NullPointerException on unboxing and hurts performance in loops.

  18. 18.Explain the Integer cache.

    Integer values from -128 to 127 are cached, so == comparison of boxed Integers appears to work in that range and fails outside it.

  19. 19.What is the difference between checked and unchecked exceptions?

    Checked exceptions are verified at compile time and must be handled or declared; unchecked exceptions extend RuntimeException and are not enforced.

  20. 20.What is try-with-resources?

    A construct that auto-closes any AutoCloseable declared in the try header, even when an exception is thrown.

  21. 21.Can a finally block be skipped?

    Only by System.exit(), a JVM crash, or an infinite loop or thread kill inside try or catch.

  22. 22.What is the difference between throw and throws?

    throw raises an exception instance at runtime; throws declares in the signature that the method may propagate one.

  23. 23.Explain the Java memory model areas.

    Heap holds objects, stack holds frames and locals per thread, metaspace holds class metadata, plus PC registers and native method stacks.

  24. 24.How does garbage collection work?

    The collector reclaims objects unreachable from GC roots, usually with generational young/old spaces and a collector such as G1 or ZGC.

  25. 25.What is a memory leak in Java?

    Objects that remain reachable but are no longer needed — typical causes are static collections, unclosed resources and listeners never deregistered.

  26. 26.What are strong, soft, weak and phantom references?

    Strong prevents collection, soft is cleared under memory pressure, weak is cleared at the next GC, and phantom is used for post-mortem cleanup.

  27. 27.Difference between final, finally and finalize?

    final marks something non-reassignable or non-extendable, finally always executes after try, finalize was a deprecated pre-GC hook.

  28. 28.What is an interface default method?

    A method with a body inside an interface, added in Java 8 so interfaces can evolve without breaking implementers.

  29. 29.Abstract class vs interface — when do you pick which?

    Use an abstract class for shared state and partial implementation; use an interface for capability contracts and multiple inheritance of type.

  30. 30.What is a functional interface?

    An interface with exactly one abstract method, so it can be implemented by a lambda — for example Runnable, Comparator or Function.

  31. 31.Explain the Stream API in one line.

    A declarative pipeline of source, intermediate lazy operations, and one terminal operation over a data sequence.

  32. 32.Difference between map() and flatMap()?

    map transforms each element one-to-one; flatMap transforms each element into a stream and flattens the result.

  33. 33.What does Optional solve?

    It makes the absence of a value explicit in the type system, reducing accidental NullPointerException.

  34. 34.What is the difference between a process and a thread?

    A process has its own memory space; threads share memory inside one process and are cheaper to create and switch.

  35. 35.How do you create a thread in Java?

    Extend Thread, implement Runnable or Callable, or — preferred — submit tasks to an ExecutorService.

  36. 36.What is the synchronized keyword doing?

    It acquires the object's monitor so only one thread executes the block, and it establishes a happens-before memory barrier.

  37. 37.Why use volatile?

    It guarantees visibility of a field's latest value across threads and prevents reordering, but it does not make compound operations atomic.

  38. 38.What is a deadlock and how do you avoid it?

    Two threads each hold a lock the other needs; avoid it with consistent lock ordering, timeouts via tryLock, or lock-free structures.

  39. 39.Runnable vs Callable?

    Runnable returns nothing and cannot throw checked exceptions; Callable returns a value and can throw.

  40. 40.What is a thread pool and why use one?

    A reusable set of worker threads that caps concurrency and removes the cost of creating a thread per task.

  41. 41.Explain CompletableFuture.

    A composable async result that supports chaining, combining and exception handling without blocking on get().

  42. 42.What is the difference between fail-fast and fail-safe iterators?

    Fail-fast iterators throw ConcurrentModificationException on structural change; fail-safe iterators work on a copy, like CopyOnWriteArrayList.

  43. 43.What is reflection used for?

    Inspecting and invoking classes, fields and methods at runtime — the basis of frameworks like Spring, Jackson and JUnit.

  44. 44.What is dependency injection?

    Objects receive their collaborators from outside instead of constructing them, which improves testability and decoupling.

  45. 45.Explain the Spring bean lifecycle briefly.

    Instantiate, populate dependencies, run aware callbacks and BeanPostProcessors, call @PostConstruct/init, use, then @PreDestroy/destroy.

  46. 46.Difference between @Component, @Service and @Repository?

    All are stereotypes registering a bean; @Service marks business logic and @Repository adds persistence exception translation.

  47. 47.What is the singleton design pattern in Java?

    One instance per JVM, typically via an enum or a static holder class, which is safest against reflection and serialisation attacks.

  48. 48.What are records in modern Java?

    Immutable data carriers that generate constructor, accessors, equals, hashCode and toString from the header.

  49. 49.What is a sealed class?

    A class that explicitly lists which types may extend it, enabling exhaustive pattern matching.

  50. 50.How would you debug high CPU in a Java service?

    Take thread dumps and correlate hot native thread IDs with jstack output, then confirm with a profiler and GC logs.

Practise these with a real interviewer

Every Tech Vista track includes mock interviews with engineers who hire for these roles.