• +91 9971497814
  • info@interviewmaterial.com

Java Interview Questions Answers

Question 1 : What is a transient variable?

Answer 1 : A transient variable is a variable that may not be serialized.

Question 2 : What is the difference  between final, finally and finalize?

Answer 2 : final is used for making a class no-subclassable, and making a member variable as a constant which cannot be modified. finally is usually 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 guaranteed of execution irrespective of occurrence 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.

Question 3 : What's the difference between the methods sleep() and wait()

Answer 3 : The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

Question 4 : This class (IncrementImpl) will be used by various threads concurrently; can you see the inherent flaw(s)? How would you improve it?

Answer 4 : public class IncrementImpl { private static int counter = 0; public synchronized void increment() { counter++; } public int getCounter() { return counter; } } The counter is static variable which is shared by multiple instances of this class. The increment() method is synchronized, but the getCounter() should be synchronized too. Otherwise the Java run-time system will not guarantee the data integrity and the race conditions will occur. The famous producer/consumer example listed at Sun's thread tutorial site will tell more. one of solutions public class IncrementImpl { private static int counter = 0; public synchronized void increment() { counter++; } public synchronized int getCounter() { return counter; } }

Question 5 : Is there any other way that you can achieve inheritance in Java?

Answer 5 : There are a couple of ways. As you know, the straight way is to "extends" and/or "implements". The other way is to get an instance of the class to achieve the inheritance. That means to make the supposed-super-class be a field member. When you use an instance of the class, actually you get every function available from this class, but you may lose the dynamic features of OOP

Question 6 : What is the immediate superclass of the Applet class?

Answer 6 : Panel

Question 7 : Is sizeof a keyword?

Answer 7 : The sizeof operator is not a keyword.

Question 8 : Which method of the Component class is used to set the position and size of a component?

Answer 8 : setBounds()

Question 9 : What are wrapper classes?

Answer 9 : Wrapper classes are classes that allow primitive types to be accessed as objects.

Question 10 : What are the high-level thread states?

Answer 10 : The high-level thread states are ready, running, waiting, and dead.

Java Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners