Java Interview Q&A for AEM Developers (3 Years Experience)
Q: What is the difference between `==` and `.equals()`?
A: `==` compares object references; `.equals()` compares object values.
Q: What’s the difference between `List`, `Set`, and `Map`?
A: `List`: Ordered, allows duplicates.
`Set`: Unordered, no duplicates.
`Map`: Key-value pairs, keys are unique.
Q: What is a `HashMap`? How does it work?
A: `HashMap` stores key-value pairs using hash codes. Handles collisions via LinkedList
or Tree (Java 8+).
Q: Difference between `ArrayList` and `LinkedList`?
A: `ArrayList` is faster for indexing; `LinkedList` is better for insert/delete operations.
Q: What is the difference between an abstract class and an interface?
A: Interfaces can't have state, support multiple inheritance. Abstract classes can have
state and method implementations.
Q: What is method overloading vs. overriding?
A: Overloading: same method name, different params.
Overriding: subclass redefines superclass method.
Q: What is encapsulation?
A: Hiding internal state using private fields and accessing them via getters/setters.
Q: What is a lambda expression?
A: Shorthand for implementing functional interfaces.
Example: `(a, b) -> a + b`
Q: What is `Optional` in Java?
A: Container to avoid `NullPointerException`. Example:
`Optional.ofNullable(obj).ifPresent(System.out::println)`
Q: What is the Stream API?
A: Processes collections functionally. E.g., `list.stream().filter().collect()`
Q: Difference between `map()` and `flatMap()`?
A: `map()` transforms values, `flatMap()` flattens nested structures.
Q: Checked vs Unchecked exceptions?
A: Checked: checked at compile-time (IOException).
Unchecked: RuntimeException.
Q: What is `try-with-resources`?
A: Auto-closes resources that implement AutoCloseable.
Q: How are collections used in Sling Models?
A: `@ValueMapValue` injects List/Map from JCR properties.
Q: How do you use `Optional` in Sling Models?
A: `Optional.ofNullable(resource.getValueMap().get("title", String.class))`
Q: How do you filter a list using Streams?
A: `list.stream().filter(e -> e != null).collect(Collectors.toList())`
Q: What is Singleton pattern?
A: Ensures one instance. OSGi services in AEM behave like Singletons.
Q: What is Dependency Injection?
A: Objects are injected instead of created inside a class. Used via `@Inject`,
`@OSGiService` in AEM.
Q: What is `final`, `finally`, and `finalize()`?
A: `final`: prevents changes.
`finally`: always runs.
`finalize()`: called before GC (deprecated).
Q: Difference between `Comparable` and `Comparator`?
A: `Comparable`: natural order inside the class.
`Comparator`: external/custom ordering.