Saturday, January 8, 2011

Q. What is the difference between a Buffered Writer and a Print Writer?


Ans. These classes were implemented for different aims. For example, Print Writer is just a convenient class to write some text information. It is convenient because it has methods to write strings, any primitives (double, float, ...) and so on, you should not convert primitive to string, some useful methods have been already implemented. 

Buffered Writer was implemented not for user convenience but for performance. The main aim of this class is to buffering characters that means if you call write(char[]) method it will flush characters to underlying outputstream only when some threshold will be reached.
Q. Why interface variables are final?


Ans. An Interface is a contract, so everything has to remain the same. If you declare a variable, it means that the variable is always available to the implementation. This is also true to the value. If someone was able to change the value, you can not depend on the value, thus breaking the contract. 


Q. Why there are two ways of implementing Thread in Java?


Please answer this question.

Q. Explain which bean is faster than other(Stateless/Statefull) and why?


Ans: State full bean has to be persisted while stateless is only used once. If cluster is used, State full Session Bean has to be replicated on all servers. State full Session Bean has to be tied to session and used for that session only, while State Less Session Bean is used for single request only. 
But, State less and State full have different usages. If you are building request response application State less is OK. If you have app that has to remember state between multiple requests, State full is the option.

Thursday, December 2, 2010

Question :Please explain Hashtable vs ConcurrentHashMap.

Answer : Hashtable and ConcurrentHashMap are both thread safe, but these classes have different implementations. ConcurrentHashMap is faster than Hashtable, the main difference is that Hashtable uses one lock for all map, but ConcurrentHashMap uses one lock per bucket therefore several threads can modify the map without blocking.

Monday, November 29, 2010

Question: What is the significance of synchronized method/block?

Answer: Synchronized works on objects, meaning that only one thread can obtain lock on a certain instance of an object. Only one thread can execute synchronized block of code on a certain object. Example of such object is list that is accessed synchronized, so that multiple threads are accessing list without corrupting its contents.

Question: Explain JVM Memory Management.

Answer: Java manages memory in 3 different places: 

Stack:
• Stacks holds method’s arguments, return value & local variable.
• FILO – First In Last Out basis. The life time of the stack members decide based on
scope.
• Initial stack size will be allocated by JVM if no arguments provide at start up.
•Stack using Push-Pop operation to manage its contents.

Static memory Area:
• This area holds all the static members of the class and also maintains run time constant pool. Run time constant pool is the table maintained by the JVM which holds string literals, constants etc.
• All the instances will share this location if multiple instances created for the class. 

Heap :
• This place holds all the real live instances & Arrays – whatever we create using new key word will get physical memory in this place.
•JVM use Garbage Collector to manage this memory location.
•Initial Heap size will be allocated by JVM if no arguments provide at start up.

Please note that all the above 3 types of memory resides in RAM.

Sunday, November 28, 2010


Question 1: How static polymorphism and dynamic polymorphism is achieved?

Answer: Static polymorphism is a compile time polymorphism and can be achieved through overloading of methods. At the time of compilation, it is known which method will execute in response to a method call. 
Dynamic polymorphism is also called run-time polymorphism. The method execution is known at run time depending upon the object being referred to . It can be achieved by overriding methods in class and in Interface implementation. As this is done dynamically so it is called dynamic polymorphism.

Question 2: Explain purpose and exact use of finally vs finalize().

Answer:
Finally:-
1) The finally block always executes when the try block exits, except System.exit(0) call.

2) Ensures that the finally block is executed even if an unexpected exception occurs.

3) Finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Finalize:-
1) This is used to clean up memory resources for object using finalize().


2) This is used just before an object is destroyed and can be called just prior to garbage collection.

Hi Friends,
Here I am going to put some common questions and their answers related to Java, J2EE and others.
Some of questions might look silly, but freshers might need their answers and I will try to improve this periodically. Some questions and their answers picked from discussion boards and I am just consolidating those questions and responses.

Your comments are valuable for me.