Thursday, October 16, 2008

JAVA INTERVIEW QUESTIONS and FAQs - Part 1

JAVA INTERVIEW QUESTIONS


Question : When you declare a method as abstract method ?

Answer : When i want child class to implement the behavior of the method.


Question : Can I call a abstract method from a non abstract method ?

Answer : Yes, We can call a abstract method from a Non abstract method in a Java abstract class


Question : What is the difference between an Abstract class and Interface in Java ? or can you explain when you use Abstract classes ?

Answer : Abstract classes let you define some behaviors; they force your subclasses to provide others. These abstract classes will provide the basic funcationality of your applicatoin, child class which inherited this class will provide the funtionality of the abstract methods in abstract class. When base class calls this method, Java calls the method defined by the child class.


An Interface can only declare constants and instance methods, but cannot implement default behavior.
Interfaces provide a form of multiple inheritance. A class can extend only one other class.
Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.
Neither Abstract classes or Interface can be instantiated.




Question : What is user-defined exception in java ?

Answer : User-defined expections are the exceptions defined by the application developer which are errors related to specific application. Application Developer can define the user defined exception by inherite the Exception class as shown below. Using this class we can throw new exceptions.




Java Example :

public class noFundException extends Exception {
}


Throw an exception using a throw statement:

public class Fund {

...
public Object getFunds() throws noFundException {

if (Empty()) throw new noFundException();
...

}
}

User-defined exceptions should usually be checked.





Question : What is the difference between checked and Unchecked Exceptions in Java ?

Answer : All predefined exceptions in Java are either a checked exception or an unchecked exception. Checked exceptions must be caught using try .. catch() block or we should throw the exception using throws clause. If you dont, compilation of program will fail.


Java Exception Hierarchy


+--------+
Object
+--------+


+-----------+
Throwable
+-----------+
/ / +-------+ +-----------+
Error Exception
+-------+ +-----------+
/ \ / \________/ \______/ +------------------+
unchecked checked RuntimeException
+------------------+
/ \_________________/

unchecked






Question : Explain garbage collection ?

Answer : Garbage collection is an important part of Java's security strategy. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects from the memory. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed.

In Java, it is good idea to explicitly assign null into a variable when no more in use.