Skip to main content

Tricky Java Interview Questions & Answers

This guide covers advanced and tricky topics frequently asked in interviews for experienced Java developers.

1. Latest Enhancements in HashMap (Java 8)

In Java 8, the internal implementation of HashMap was improved to handle collisions more efficiently.

  • Balanced Trees: When a specific threshold (TREEIFY_THRESHOLD = 8) is reached in a single bucket, the linked list is converted into a Balanced Tree (Red-Black Tree).
  • Performance: This changes the worst-case time complexity from O(n) to O(log n), significantly improving performance for high-collision scenarios.

2. Optional Class in Java 8

Optional<T> is a container object which may or may not contain a non-null value.

  • Purpose: It is used to prevent NullPointerException and provide a more expressive way to handle missing values.
  • Key Methods: isPresent() checks if a value exists, and get() retrieves it.

3. Map vs. FlatMap

  • Map: Performs a one-to-one transformation. For every input element, it produces exactly one output element.
  • FlatMap: Performs a one-to-many transformation. It flattens a stream of streams into a single stream.
    • Example: Use map to increment a list of salaries; use flatMap to extract all book titles from a list of employees where each employee has a list of books.

4. Factory vs. Abstract Factory Pattern

  • Factory Pattern: Creates objects of a related type through a single factory method.
  • Abstract Factory Pattern: A "factory of factories." It provides an interface for creating families of related or dependent objects without specifying their concrete classes.

5. Spring Bean Scopes

  1. Singleton (Default): One instance per Spring IoC container.
  2. Prototype: A new instance every time the bean is requested.
  3. Request: One instance per HTTP request (Web-aware).
  4. Session: One instance per HTTP session (Web-aware).
  5. Global Session: One instance per global HTTP session (Portlet context).

6. Spring AOP (Aspect Oriented Programming)

AOP helps in segregating cross-cutting concerns (like logging, security, or transactions) from the main business logic.

  • Aspect: A class containing the cross-cutting logic.
  • Advice: The action taken (the method).
  • Pointcut: An expression that defines where the advice should be applied.
  • Join Point: A specific point in the application execution (like a method call).

7. MetaSpace vs. PermGen

  • PermGen (Before Java 8): Part of the Heap memory with a fixed size. It often led to OutOfMemoryError.
  • MetaSpace (Java 8+): Replaced PermGen. It uses Native Memory (RAM) rather than heap memory. It is auto-resizable and grows dynamically, reducing memory overflow issues.

8. Multi-Catch Block (Java 7+)

Allows you to handle multiple types of exceptions in a single catch block using the pipe (|) operator.

try {
// code
} catch (NullPointerException | ArithmeticException e) {
logger.error(e.getMessage());
}

9. Cascade Types in JPA/Hibernate

Cascading allows you to propagate operations from a parent entity to its child entities.

  • PERSIST: Saving the parent saves the child.
  • MERGE: Updating the parent updates the child.
  • REMOVE: Deleting the parent deletes the child.
  • DETACH/REFRESH: Propagates detaching or refreshing.
  • ALL: Applies all the above.

10. Why Microservices?

  • Resilience: Failure in one service doesn't break the whole system.
  • Scalability: You can scale only the specific services that need more resources.
  • Technology Diversity: Different services can use different languages/frameworks.
  • Faster Time to Market: Teams can develop and deploy services independently.