41. When does an Object becomes eligible for Garbage collection
in Java ?
A Java object is subject to garbage collection when it
becomes unreachable to the program in which it is currently used.
42. Does Garbage collection occur in permanent generation space
in JVM ?
Garbage Collection does occur in PermGen space and if
PermGen space is full or cross a threshold, it can trigger a full garbage
collection. If you look carefully at the output of the garbage collector, you
will find that PermGen space is also garbage collected. This is the reason why
correct sizing of PermGen space is important to avoid frequent full garbage
collections. Also check our article Java 8: PermGen to Metaspace.
Exception
Handling
43. What are the two types of Exceptions in Java ? Which are the
differences between them ?
Java has two types of
exceptions: checked exceptions and unchecked exceptions. Unchecked exceptions
do not need to be declared in a method or a constructor’s throws clause, if
they can be thrown by the execution of the method or the constructor, and
propagate outside the method or constructor boundary. On the other hand,
checked exceptions must be declared in a method or a constructor’s throws
clause. See here for tips on Java exception handling.
44. What is the difference between Exception and Error in java
?
Exception and Error classes
are both subclasses of the Throwable class.
The Exception class
is used for exceptional conditions that a user’s program should catch.
The Error class
defines exceptions that are not excepted to be caught by the user program.
45. What is the difference between throw and throws ?
The
throw keyword is used to explicitly raise a exception within the program. On
the contrary, the throws clause is used to indicate those exceptions that are
not handled by a method. Each method must explicitly specify which exceptions
does not handle, so the callers of that method can guard against possible
exceptions. Finally, multiple exceptions are separated by a comma.
45. What is the importance of finally block in exception
handling ?
A finally block will always be executed, whether or not an
exception is actually thrown. Even in the case where the catch statement is
missing and an exception is thrown, the finally block will still be executed.
Last thing to mention is that the finally block is used to release resources
like I/O buffers, database connections, etc.
46. What will happen to the Exception object after exception
handling ?
The Exception object
will be garbage collected in the next garbage collection.
47. How does finally block differ from finalize() method ?
A
finally block will be executed whether or not an exception is thrown and is
used to release those resources held by the application. Finalize is a
protected method of the Object class, which is called by the Java Virtual
Machine (JVM) just before an object is garbage collected.
Java
Applets
48. What is an Applet ?
A java applet is
program that can be included in a HTML page and be executed in a java enabled
client browser. Applets are used for creating dynamic and interactive web
applications.
49. Explain the life cycle of an Applet.
An
applet may undergo the following states:
·
Init:
An applet is initialized each time is loaded.
·
Start:
Begin the execution of an applet.
·
Stop:
Stop the execution of an applet.
·
Destroy:
Perform a final cleanup, before unloading the applet.
50. What happens when an applet is loaded ?
First
of all, an instance of the applet’s controlling class is created. Then, the
applet initializes itself and finally, it starts running.
No comments:
Post a Comment