Interview Questions 1 - 10  «Prev Next»

Java Interview: Reflection, Memory, and Web Concurrency

Review JavaBeans introspection, object graphs, leak diagnosis, abstraction, virtual threads, strict floating point, and Spring REST support.

  1. How does JavaBeans introspection use reflection?

    Answer: Introspector discovers bean properties, methods, and events using naming conventions and optional BeanInfo metadata. A getter/setter pair can describe a property without exposing a field directly. Reflection does not automatically bypass access checks or module boundaries.

    import java.beans.Introspector;
    
    public class BeanInspection {
        public static final class PersonBean {
            private String name;
            public String getName() { return name; }
            public void setName(String name) { this.name = name; }
        }
        public static void main(String[] args) throws Exception {
            PersonBean person = new PersonBean();
            person.setName("Ada");
            var info = Introspector.getBeanInfo(PersonBean.class, Object.class);
            for (var property : info.getPropertyDescriptors()) {
                System.out.println(property.getName());
                System.out.println(property.getReadMethod().invoke(person));
            }
        }
    }
    

    JavaBeans introspection belongs to the java.desktop module. Record accessors such as name() are not automatically equivalent to the traditional getName/setName bean convention.

  2. What is an object graph?

    Answer: It is a model in which objects are nodes and their references are edges. A graph can contain shared references and cycles. Garbage collectors reason about reachability through such relationships, while persistence mechanisms decide which relationships to store according to their own rules.

    Java serialization traverses eligible non-transient instance state by default, preserving sharing and cycles, but custom serialization can change that behavior. Neither serialization nor a persistence framework necessarily stores every reachable object in one file.

  3. How can you investigate a Java memory leak?

    Answer: Look for unwanted retention: objects remain reachable after their useful lifetime. Compare memory behavior under a repeatable workload, inspect heap histograms or heap dumps, and follow paths to garbage-collection roots. Unbounded caches, listeners, queues, and thread-local values are common places to investigate.

    Java Flight Recorder and tools such as jcmd can help collect evidence. An OutOfMemoryError alone does not prove a leak, and StackOverflowError commonly points to excessive recursion. PermGen is obsolete in modern HotSpot; consider heap, metaspace, direct/native memory, and thread usage separately.

  4. How do front-end, back-end, and full-stack roles differ?

    Answer: Front-end work focuses on the user-facing interface and client behavior, including accessibility and browser performance. Back-end work focuses on server-side behavior, APIs, data, and operational concerns. Full-stack work crosses both areas.

    The exact boundaries vary by team. These labels do not imply that all large organizations require separate roles or that one person must be an expert in every layer.

  5. How do abstraction and encapsulation differ?

    Answer: Abstraction defines the meaningful behavior clients use. Encapsulation groups implementation and state behind a boundary and helps enforce its rules. A queue interface is an abstraction; its private backing array and controlled operations are part of its encapsulation.

    They support each other but are not synonymous. Classes and ordinary methods can provide abstractions without being declared abstract.

  6. Why can an abstract class not be directly instantiated?

    Answer: The language treats it as an incomplete or intentionally non-instantiable base. It may contain abstract operations, but even one with no abstract methods cannot be directly constructed. A concrete subclass can be instantiated and invokes superclass construction as part of initialization.

    An anonymous subclass of an abstract class can be created if it supplies the required implementations. That creates the concrete anonymous subclass, not a direct instance of the abstract class.

  7. What are useful scenarios for Java concurrency?

    Answer: Examples include overlapping independent network calls, processing background jobs, serving concurrent requests, and dividing CPU-intensive work across processors. The execution strategy should fit the workload: bounded parallelism for CPU work, and potentially virtual threads for large numbers of blocking tasks.

    Coordinate shared state, propagate failures, and bound access to scarce resources such as database connections. A servlet container can invoke the same servlet instance concurrently, so per-request data should not be stored in unprotected shared instance fields.

  8. What are the tradeoffs of thread-per-request processing?

    Answer: Using one platform thread per blocked request can limit scalability because platform threads consume memory and scheduling resources. Servlet asynchronous processing and non-blocking I/O can release request threads while work waits. These features require an appropriate application and container design; they do not automatically make every servlet non-blocking.

    Virtual threads provide another way to support many blocking tasks with lower thread overhead. Thread-per-request is therefore not inherently wrong. Virtual threads still require resource limits and do not speed up CPU-bound computation; asynchronous Servlet support and thread selection are separate concerns.

  9. What does strictfp do in Java SE 25?

    Answer: It has no additional effect on floating-point evaluation. Since Java 17, Java always uses strict floating-point semantics. The keyword remains accepted for compatibility, but the compiler can warn that it is unnecessary.

    Strict semantics do not make binary floating point exact decimal arithmetic or eliminate rounding, NaN, and infinities. Use a numerical representation that fits the application's requirements.

  10. What support does Spring provide for REST services?

    Answer: Spring MVC supplies annotated request mappings, parameter binding, validation integration, response status handling, and message conversion. RestController combines Controller with ResponseBody semantics so return values are written to the response through converters. ResponseEntity gives explicit control over status, headers, and body.

    Spring WebFlux provides a reactive web stack with different execution assumptions. RestClient and WebClient support outbound HTTP calls. These are Spring APIs rather than Java SE features; use compatible framework dependencies and the documentation for the selected release.

References: Introspector, Thread, Java SE 25 Language Specification, Jakarta Servlet 6.1, JDK 25 jcmd, Spring response-body handling, Spring REST clients.

SEMrush Software