Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
57 views

Master List of Java Questions: What Is Exception ?

The document discusses exception handling and garbage collection in Java. It defines exceptions as abnormal program behaviors and distinguishes between user-defined and built-in exceptions. It explains that the garbage collector automatically manages memory by periodically identifying and releasing unused objects, though it does not guarantee a program will not run out of memory. Finally blocks contain cleanup code that always executes regardless of exceptions, and exceptions can be checked or unchecked depending on whether they need to be declared.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Master List of Java Questions: What Is Exception ?

The document discusses exception handling and garbage collection in Java. It defines exceptions as abnormal program behaviors and distinguishes between user-defined and built-in exceptions. It explains that the garbage collector automatically manages memory by periodically identifying and releasing unused objects, though it does not guarantee a program will not run out of memory. Finally blocks contain cleanup code that always executes regardless of exceptions, and exceptions can be checked or unchecked depending on whether they need to be declared.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Master List of Java Questions:

Exception Handling and Garbage Collection

1. What is Exception ?

An exception is an abnormal behavior existing during a normal execution of a


program. For example: When writing to a file if there does not exist required file
then an appropriate exception will be thrown by java code

2. What is a user-defined exception?

For every project you implement you need to have a project dependent exception
class so that objects of this type can be thrown so in order to cater this kind of
requirement the need for user defined exception class is realized.
for example:

class MyException extends Exception{


public MyException(){};
public MyException(String msg){
super(msg);
}

3. What do you know about the garbage collector?

In Java, memory management is done automatically by JVM.A programmer is


free of this responsibility of handling memory. A garbage collector is a part of
JVM responsible for removing objects from heap, which is no longer in use. The
garbage collector typically runs in a background thread, periodically scanning the
heap, identifying garbage objects, and releasing the memory they occupy so that
the memory is available for future objects.

4. Why Java does not support pointers?

As per the design decision Java does not support pointers explicitly.This greatly
reduces the burden of dynamic memory management while coding from
programmers.Though programmers dynamically allocate memory while coding
but they need not worry about deallocating this memory.The automatic garbage
collection feature of Java collects dangling references of objects though it has a
trade off on performance as programmer managed memory management will be
efficient as compared to JVM driven automatic garbage collection.

5. Does garbage collection guarantee that a program will not run out of memory?

Garbage collection does not guarantee that a program will not run out of memory.
As garbage collection is JVM dependent then It is possible for programs to use
memory resources faster than they are garbage collected.Moreover garbage
collection cannot be enforced,it is just suggested.Java guarantees that the finalize
method will be run before an object is Garbage collected,it is called by the
garbage collector on an object when garbage collection determines that there are
no more references to the object.
The garbage collection is uncontrolled, it means you cannot predict when it will
happen, you thus cannot predict exactly when the finalize method will run. Once a
variable is no longer referenced by anything it is available for garbage
collection.You can suggest garbage collection with System.gc(), but this does not
guarantee when it will happen.

6. What is finally in Exception handling?

finally' is a part of try-catch-throw and finally blocks for exception handling


mechanism in Java.'finally' block contains snippet which is always executed
irrespective of exception occurrence. The runtime system always executes the
statements within the finally block regardless of what happens within the try
block. The cleanup code is generally written in this part of snippet e.g. dangling
references are collected here.

7. What can prevent the execution of the code in finally block?

-Use of System.exit()
-The death of thread
-Turning off the power to CPU
-An exception arising in the finally block itself

8. Explain 'try','catch' and 'finally' blocks?


In Java exceptions are handled in try, catch, throw and finally blocks. It says try a
block of Java code for a set of exception/s catch an exception if it appears in a
catch block of code separate from normal execution of code. It clearly segregates
errors from a block of code in an effective and efficient manner. The exceptions,
which are caught, thrown using throw keyword. A finally block is called in order
to execute clean up activities for any mess caused during abnormal execution of
program.

9. Define Checked and Unchecked exception.

A checked exception is one, which a block of code is likely to throw, and


represented by throws clause.It represents invalid conditions in areas outside the
immediate control of the program (invalid user input, database problems, network
outages, absent files).
In Java it is expected that a method 'throws' an exception which is a checked
exception.They are subclasses of Exception.
While unchecked exceptions represent defects in the program (often invalid
arguments passed to a non-private method).
According to definition in The Java Programming Language, by Gosling, Arnold,
and Holmes,"Unchecked runtime exceptions represent conditions that, generally
speaking, reflect errors in your program's logic and cannot be reasonably
recovered from at run time." They are subclasses of RuntimeException, and are
usually implemented using IllegalArgumentException, NullPointerException, or
IllegalStateException
It is somewhat confusing, but note as well that RuntimeException (unchecked) is
itself a subclass of Exception (checked).

You might also like