Java Interview Questions
Advanced Java & Garbage Collection
Q1. What is the difference between Checked and Unchecked
Exceptions?
Checked exceptions (e.g., IOException, SQLException) are checked at compile-time,
forcing the developer to catch or declare them. Unchecked exceptions (e.g.,
NullPointerException, ArithmeticException) inherit from RuntimeException and are not
checked at compile-time.
Q2. How does Garbage Collection work in Java?
Garbage Collection (GC) automatically manages memory by reclaiming memory
occupied by objects that are no longer reachable by any active thread. Common
algorithms involve identifying 'GC roots', marking reachable objects, and sweeping
(deleting) the unmarked objects.
Q3. Difference between throw and throws?
'throw' is a keyword used inside a method body to explicitly trigger an exception object.
'throws' is used in the method signature to declare that the method might throw one or
more exceptions, delegating the handling to the caller.
Q4. Can we catch an Error in Java?
While it is syntactically possible to catch an Error (since it extends Throwable), it is
highly discouraged. Errors (like OutOfMemoryError, StackOverflowError) represent
severe, unrecoverable system conditions that standard applications should not attempt
to handle.
Q5. What is the try-with-resources statement?
Introduced in Java 7, try-with-resources is a try block that declares one or more
resources (objects that implement AutoCloseable). It ensures that each resource is
automatically closed at the end of the statement, preventing resource leaks without
needing complex 'finally' blocks.
Q6. What is a ClassLoader in Java?
A ClassLoader is part of the JRE that dynamically loads Java classes into the JVM
when they are first referenced. The primary class loaders are the Bootstrap, Extension
(Platform), and Application (System) ClassLoaders.
Q7. What is a Memory Leak in Java and how to prevent it?
A memory leak occurs when an application unintentionally holds references to objects
that are no longer needed, preventing the GC from destroying them. Prevent it by using
weak references, closing streams/connections, and removing objects from static
collections when done.
Q8. What is the difference between Stack and Heap memory?
Stack memory is used for execution of a thread; it contains local variables and method
call frames. It is fast and follows LIFO. Heap memory is used for dynamic allocation of
objects and JRE classes. Objects in the heap have global access and are managed by
the GC.
Q9. What are Annotations in Java?
Annotations provide metadata about the program that is not part of the program itself.
They can be used by the compiler to detect errors (e.g., @Override), by tools to
generate code/XML, or at runtime via reflection (e.g., Spring/Hibernate annotations).
Q10. What is the finalize() method?
finalize() is a method of the Object class called by the Garbage Collector just before an
object is destroyed. It was intended for resource cleanup but is highly unpredictable and
deprecated since Java 9. try-with-resources or Cleaners are now the recommended
alternatives.
Generated Document • Advanced Java & Garbage Collection