Top 20 Java OOP Interview Questions and Answers

Top 20 Java OOP Interview Questions and Answers Work through these 20 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.

  1. Explain the difference between == and .equals() in Java.

    For primitives, == compares values. For objects, == asks if two references point to the same instance. equals() compares content when overridden — String overrides it for...

    Full answer

  2. What is dependency injection and why does Spring use it?

    DI supplies collaborators from outside the class — typically via constructor. Problem solved: tight coupling to concrete classes, hard unit tests, scattered object...

    Full answer

  3. @RestController vs @Controller?

    @Controller is for traditional MVC — return "user/list" view name. @RestController adds @ResponseBody so return types become JSON/XML body — standard for REST APIs. Use...

    Full answer

  4. What is the difference between Comparable and Comparator?

    Comparable built into the type — Employee implements compareTo by id. Comparator external — sort by name or salary without changing Employee. Use...

    Full answer

  5. What is the difference between JPA and Hibernate?

    JPA is the API contract; Hibernate is the engine under Spring Boot by default. You write @Entity per JPA spec; Hibernate handles SQL generation. Spring Data JpaRepository adds...

    Full answer

  6. Explain method overloading vs overriding.

    Overload print(int) and print(String) in one class — compiler picks at compile time. Override toString in subclass — JVM calls subclass version via virtual method table....

    Full answer

  7. Explain sealed classes and their purpose in Java 17+.

    sealed interface Shape permits Circle, Rectangle limits who implements Shape. Compiler verifies switch (Shape s) covers all cases — no default needed. Use for domain variants...

    Full answer

  8. What is the Java Platform Module System (JPMS) and who needs it?

    Use modules when building custom JDK images or publishing strict API boundaries. Typical microservice: no module-info — Spring Boot repackages classpath. Watch for illegal...

    Full answer

  9. How does Bean Validation work in Spring Boot 3?

    DTO fields: @NotBlank, @Email, @Size. Controller: createUser(@Valid @RequestBody UserDto dto). Global @RestControllerAdvice maps MethodArgumentNotValidException to 400 + field...

    Full answer

  10. Explain @ConfigurationProperties vs @Value.

    @ConfigurationProperties(prefix = "app.payment") record PaymentProps(int timeout, String apiUrl) {} — validated, testable, grouped. Use @Value only for single feature flags....

    Full answer

  11. BeanFactoryPostProcessor vs BeanPostProcessor — explain both.

    BFPP: replace ${placeholder} in definitions via PropertySourcesPlaceholderConfigurer. BPP: @Autowired injection, @PostConstruct detection, AOP proxy wrapping. Custom BPP can...

    Full answer

  12. When use @Query native SQL vs JPQL in Spring Data JPA?

    JPQL for standard entity fetches and portable logic. Native for PostgreSQL-specific jsonb queries, recursive CTEs, or performance-tuned index hints after EXPLAIN. Interface...

    Full answer

  13. What is Metaspace and how do class loading issues cause memory leaks?

    Production set MaxMetaspaceSize 256m with monitoring. Investigate Metaspace OOM: heap dump + classloader histogram. Avoid generating unlimited dynamic proxies. OSGi/script...

    Full answer

  14. What do Java Records provide and their limitations?

    DTO: record UserResponse(String id, String name) {} — JSON mapping works. Service layer immutable value objects. Not for entities needing setters/lazy proxies yet. Combine with...

    Full answer

  15. Why does Context re-render every consumer, and how do you keep RBAC/theme from stalling the tree?

    AuthProvider holds user with useMemo. Permission checks go through can(permission) from a hook that reads a stable access object. Theme is a separate provider. High-frequency...

    Full answer

  16. What do Error Boundaries catch, and what do they miss?

    Route-level boundary with a retry that resets the error state. Widget-level boundary around a risky chart. Query/mutation errors stay in the data layer (toast + field errors)....

    Full answer

  17. A table of 10,000 rows janks on scroll. What do you change first?

    I first ask if the API can page or cursor. If the UX needs a long scroll, I virtualize, give each row a stable id, and keep cell renderers pure. I profile with React DevTools —...

    Full answer

  18. What are hidden classes in JDK 15, and which runtime frameworks rely on them?

    I do not write hidden classes in business code. I know JDK 15+ lambdas and several proxy libraries use them so generated bytecode unloads cleanly. If a heap dump shows...

    Full answer

  19. What are sequenced collections in JDK 21, and which calls replace fragile index access?

    I reach for getFirst/getLast on lists and deques instead of magic indexes. For a recent-activity map I use LinkedHashMap as a SequencedMap and pollFirstEntry to evict oldest....

    Full answer

  20. What did generational ZGC change in JDK 21, and when would you pick it over G1?

    For a large-heap, latency-sensitive API I trial generational ZGC in staging: p99 pause, allocation rate, and RSS. I keep G1 if throughput or CPU is the SLO. I never flip...

    Full answer

Java OOP Interview Questions and Answers · Top 50 Java OOP Interview Questions and Answers (2026 Edition) · Top 40 Java OOP Interview Questions | Must-Prepare before interview. · Java OOP Interview Questions Every Fresher Must Know

All interview question lists · Interview guides · Home · Support the platform