Chekout the new Some more Core Java Questions I usually ask for the Entry level people in J2EE field. New
Here are few interview questions in core Java that I usually ask while interviewing...
![]() | |
|
1. What are the different types of inner classes? [Ans] There are four different types of inner classes in Java. They are: a)Static member classes , a static member class has access to all static methods of the parent, or top-level, class b) Member classes, the member class is instance specific and has access to any and all methods and members, even the parent's this reference c) Local classes, are declared within a block of code and are visible only within that block, just as any other method variable. d) Anonymous classes, is a local class that has no name | |
|
2. In which case would you choose a static inner class? [Ans]Interesting one, static inner classes can access the outer class's protected and private fields. This is both a positive and a negitive point for us since we can, in essence, violate the encapsulation of the outer class by mucking up the outer class's protected and private fields. The only proper use of that capability is to write white-box tests of the class -- since we can induce cases that might be very hard to induce via normal black-box tests (which don't have access to the internal state of the object). Second advantage,if I can say, is that, we can this static concept to impose restriction on the inner class. Again as discussed in earlier point, an Inner class has access to all the public, private and protected members of the parent class. Suppose you want to restrict the access even to inner class, how would you go ahead? Making the inner class static enforces it to access only the public static members of the outer class( Since, protected and private members are not supposed to be static and that static members can access only other static members). If it has to access any non-static member, it has to create an instance of the outer class which leads to accessing only public members. |
|
|
|
|
|
3. What is the differnce between final, finally and finalize? [Ans] final is used for making a class no-subclassable, and making a member variable as a constant which cannot be modified. finally is usuall used to release all the resources utilized inside the try block. All the resources present in the finalize method will be garbage collected whenever GC is called. Though finally and finalize seem to be for a similar task there is an interesting tweak here, usually I prefer finally than finalize unless it is unavoidable. This is because the code in finally block is guranteed of execution irrespective of occurance of exception, while execution of finalize is not guarenteed.finalize method is called by the garbage collector on an object when the garbage collector determines that there are no more references to the object. Presumably the garbage collector will, like its civil servant namesake, visit the heap on a regular basis to clean up resources that are no longer in use. Garbage collection exists to prevent programmers from calling delete. This is a wonderful feature. For example, if you can't call delete, then you can't accidentally call delete twice on the same object. However, removing delete from the language is not the same thing as automatically cleaning up. To add to it, Garbage collection might not ever run. If garbage collection runs at all, and an object is no longer referenced, then that object's finalize will run. Also, across multiple objects, finalize order is not predictable. The correct approach to resource cleanup in Java language programs does not rely on finalize. Instead, you simply write explicit close methods for objects that wrap native resources. If you take this approach, you must document that the close method exists and when it should be called. Callers of the object must then remember to call close when they are finished with a resource. |
|
|
|
|
|
4. What is externalization? Where is it useful? [Ans]Use the Externalizable interface when you need complete control over your Bean's serialization (for example, when writing and reading a specific file format). |
|
|
5. Explain the Exception heirarchy in Java. [Ans]
|
|
![]() |
Chekout the new Some more Core Java Questions I usually ask for the Entry level people in J2EE field. New
| Email your feedback to Girish |
Disclaimers: No claims are made about the accuracy of any of the document presented in this site and no responsibility is taken for any errors. All the links provided in this site are individual compilation, however no claims are made about the accuracy and authenticity of the respective contents.