Section_3_Java_Core_True_Senior_H1_H2
Section_3_Java_Core_True_Senior_H1_H2
Java Core
How does the Java Memory Model (JMM) affect multi-threaded program
behavior?
JMM defines how threads interact through memory and when writes by one thread
become visible to others.
It specifies happens-before relationships to avoid visibility and reordering issues.
Volatile, synchronized, and final fields are tools to enforce memory visibility guarantees.
Reordering by the compiler, CPU, or JIT is legal unless constrained by JMM rules.
Understanding JMM is essential to avoid subtle concurrency bugs like stale reads or
instruction reordering.
How would you analyze and resolve a high GC pause issue in a production Java
application?
Identify GC algorithm in use (e.g., G1, ZGC) and analyze logs for frequency and pause
duration.
Use tools like GCViewer, JFR, or GCeasy to visualize heap regions and promotion
failures.
Check object allocation rate, large tenured objects, and excessive promotion into old
gen.
Tune heap size, region sizes, and pause goals based on profiling data.
Refactor code to reduce allocation pressure or cache usage that causes memory
retention.
How do you implement immutability effectively in Java and what are its
performance implications?
Use final fields and private constructors, and avoid setters or mutable references.
Defensive copying is necessary for collections or nested mutable objects.
Immutability reduces synchronization needs and is naturally thread-safe.
Overhead comes from frequent object creation; use builders or reuse instances when
possible.
Works well with functional programming patterns and improves cache predictability.
What are the trade-offs between using primitive types vs boxed types in Java?
Primitives are faster and memory-efficient; boxed types introduce GC overhead and
autoboxing latency.
Boxed types are necessary in collections and generics but can lead to
NullPointerExceptions.
Avoid unnecessary boxing in hot paths or numeric computations (e.g., avoid Double in
loops).
Use specialized collections (e.g., Trove, fastutil) when performance matters.
Profile and monitor heap allocation to identify boxing hotspots in production.