0% found this document useful (0 votes)
8 views3 pages

Java Interview Advanced

The document provides advanced Java interview questions focusing on exceptions, garbage collection, and memory management. Key topics include the differences between checked and unchecked exceptions, the workings of garbage collection, and the use of try-with-resources for resource management. It also discusses class loaders, memory leaks, stack vs. heap memory, annotations, and the deprecated finalize() method.

Uploaded by

gefehif698
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Java Interview Advanced

The document provides advanced Java interview questions focusing on exceptions, garbage collection, and memory management. Key topics include the differences between checked and unchecked exceptions, the workings of garbage collection, and the use of try-with-resources for resource management. It also discusses class loaders, memory leaks, stack vs. heap memory, annotations, and the deprecated finalize() method.

Uploaded by

gefehif698
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like