Java Interview Questions : Series 6
100. What is Garbage Collection in
Java?
Java has an internal mechanism called Garbage collection to
reclaim the memory of unused projects at run time.
Garbage collection is also known as automatic memory
management.
101. Why Java provides Garbage
Collector?
In Java, there are no pointers. Memory management and allocation
is done by JVM. Since memory allocation is automated, after some
time JVM may go low on memory. At that time, JVM has to free
memory from unused objects. To help with the process of
reclaiming memory, Java provides an automated process called
102. What is the purpose of gc() in
Java?
Java provides two methods System.gc() and Runtime.gc() to request
the JVM to run the garbage collection. By using these methods,
programmers can explicitly send request for Garbage Collection.
But JVM process can reject this request and wait for some time
before running the GC.
103. How does Garbage Collection
work in Java?
Java has an automated process called Garbage Collector for
Memory Management. It is a daemon in JVM that monitors the
memory usage and performs memory cleanup. Once JVM is low on
memory, GC process finds the unused objects that are not
referenced by other objects. These unused objects are cleaned up by
Garbage Collector daemon in JVM.
104. When does an object become
eligible for Garbage Collection in
Java?
An object can be Garbage Collected by JVM, if it is not reachable.
There are two cases for deciding eligibility of objects for Garbage
Collection:
1. An Object/instance that cannot be reached by a live thread.
2. A set of circularly referenced instances that cannot be
reached by any other instance outside that set.
105. Why do we use finalize() method
in Java?
Java provides finalize() method to perform any cleanup before
Garbage Collection. This method is in Object class, and it is
invoked by JVM internally. Developers are free to implement this
method for any custom cleanup in case of Garbage Collection.
If an Object is not Garbage Collected, then this method may not be
called.
This method is never invoked more than once by JVM.
106. What are the different types of
References in Java?
In Java, there are four types of references:
1. Strong Reference
2. Soft Reference
3. Weak Reference
4. Phantom Reference
107. How can we reference an
unreferenced object again?
We can provide implementation in finalize() method to reference
and unreferenced object. For an unreferenced object, finalize()
method is called at the time of Garbage Collection. At this time,
Object can pass its reference ‘this’ to finalize() method and revive
itself.
108. What kind of process is the
Garbage collector thread?
Garbage Collection is a Daemon process in JVM. It is an internal
process that keep checking Memory usage and cleans up the
memory.
109. What is the purpose of the
Runtime class?
The purpose of the Runtime class is to provide access to the Java
Runtime system. This class provides certain important methods like:
1. Runtime.freeMemory() – This method returns the value of
free memory in JVM
2. Runtime.maxMemory() - This method returns the value of
maximum memory that JVM can use.
3. Runtime.gc() – This method can invoke garbage collection.
110. How can we invoke an external
process in Java?
Java provides the method Runtime.getRuntime().exec() to invoke an
external process from JVM.
111. What are the uses of Runtime
class?
Runtime class in Java provides following benefits:
1. It allows to read data via key board
2. It can use system properties and environment variables
3. It helps in running non-java programs from within a java
application.
112. What is a Nested class?
In Java, a Nested class is a class declared inside another class. We
can have more than one class declared inside a file.
113. How many types of Nested classes
are in Java?
Java provides four types of Nested classes:
1. Member inner class
2. Local inner class
3. Anonymous inner class
4. Static nested class
114. Why do we use Nested Classes?
There are following reasons for using nested classes:
1. Logical Grouping: We can logically group classes in one
place. If one class is useful to only one other class, then
we put smaller class within the larger class and keep them
in one file. This kind of nesting "helper classes" in a toplevel
class makes the package more streamlined.
2. Encapsulation: Nested classes increase encapsulation. Let
say there are two top-level classes, Foo and Bar. Bar
needs access to private members of Foo. We can hide
class Bar within class Foo. In this way, private members
of Foo can be accessed by class Bar. So class Foo remains
encapsulated. Also, class Bar remains hidden from the
outside world.
3. Code Clarity: Nested classed make the code more
readable and well organized. Only Top-level classes are
exposed. The helper classes are kept hidden and closer the
code where it is used by a Top-level class.
115. What is the difference between a
Nested class and an Inner class in
Java?
An Inner class in Java is non-static class. It is a type of Nested class
that is defined in another class but not qualified with a Static
modifier. A Nested class is also a class can be Static Nested class
or a non-Static Inner class.
An Inner class has access to other members of the enclosing class,
even if they are declared private. A Static Nested class can not
access the other members of the enclosing class.
116. What is a Nested interface?
A Nested interface is declared inside another interface or a toplevel
class. By default it is static.
A Nested interface is also known as Static interface.
117. How can we access the non-final
local variable, inside a Local Inner
class?
Java allows a Local Inner class to access only Constant local
members. So we have to make the non-final local variable as final
constant to access it inside a Local Inner class.
118. Can an Interface be defined in a
Class?
Yes, we can define a Static Nested interface within a class. Only the
enclosing class can access it.
119. Do we have to explicitly mark a
Nested Interface public static?
A Nested Interface is implicitly public static. So the modifiers
public and static are redundant in declaration.
120. Why do we use Static Nested
interface in Java?
Only the enclosing class can access a Static Nested interface.
Consider following code in which interface Xyz is enclosed in
class Abc.
public class Abc {
public interface Xyz {
void callback();
}
public static void registerCallback(Xyz xyz) {...}
}
// Client Code
Abc.registerCallback(new Abc.Xyz() {
public void callback() {...}
});
Any code that cannot access Abc can not access interface Xyz also.
So the purpose of declaring an Inner interface is to restrict its
access from outside world.

Comments
Post a Comment