Monday, February 8, 2010

Common Interview Questions

1)    Is JVM a compiler or an interpreter? - Interpreter

2)    What is reflection?
    - Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.

3)    What is the finalize method do?
     - Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.

4)    What is garbage collection? What is the process that is responsible for doing that in java?
     - Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process

5)    What kind of thread is the Garbage collector thread?
     - It is a daemon thread.

6)    What is the final keyword denotes?
     - final keyword denotes that it is the final implementation for that method or variable or class. You can’t override that method/variable/class any more.

7)    What is the major difference between LinkedList and ArrayList?
     - LinkedList are meant for sequential accessing. ArrayList are meant for random accessing.

8)    What is nested class?
     - If all the methods of a inner class is static then it is a nested class.

9)    What is inner class?
     - If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class.

10) What is composition?
     - Holding the reference of the other class within some other class is known as composition.

11) What is aggregation?
     - It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation.

12) What is the basic difference between the 2 approaches to exception handling.
1.try catch block and
2. specifying the candidate exceptions in the throws clause?
     
      In the first approach as a programmer of the method, you urself are dealing with the exception. This is fine if you are in a best position to decide should be done in case of an exception. Whereas if it is not the responsibility of the method to deal with it's own exceptions, then do not use this approach. In this case use the second approach. In the second approach we are forcing the caller of the method to catch the exceptions, that the method is likely to throw. This is often the approach library creators use. They list the exception in the throws clause and we must catch them. You will find the same approach throughout the java libraries we use.

13) Is it necessary that each try block must be followed by a catch block?
              
            It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

14) If I write return at the end of the try block, will the finally block still execute?

Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.



15)  If I write System.exit (0); at the end of the try block, will the finally block still execute?
 
No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.

No comments:

Post a Comment