Professional Documents
Culture Documents
Java Interview Questions & Answers by BestOnlineTrainers
Java Interview Questions & Answers by BestOnlineTrainers
By www.bestonlinetrainers.com
www.bestonlinetrainers.com
Q12: State the purpose of garbage collection in Java, and also when is it used?
Answer: Identifying and discarding objects that are no longer needed by a program so that their resources can be reclaimed and reused is the purpose of garbage collection. When a Java object is subjected becomes unreachable to the program in which it is used; it is subjected to garbage collection.
www.bestonlinetrainers.com
Q15: State which is better: to synchronize critical section of getInstance() method or whole getInstance() method ?
Answer: Here the answer is a bit critical as if we lock the whole method than every time then this method will be called and would have to wait even though any object is not being created.
Q16: When String gets created using literal or new() operator , what difference is seen?
Answer: When a string is created with new() its created in heap and not added into string pool while using literal , String can be created in String pool itself which exists in Perm area of heap.
Q17: Does overriding HashCode() method have any performance implication or not ?
Answer: Does overriding HashCode() method have any performance implication or not ?
Q18: In multithreaded environment whats wrong in using HashMap? When does get() method go to infinite loop ?
Answer: This happens during concurrent access and re-sizing.
Q19: What is thread-safety? Why is it required? And how to achieve threadsafety in Java Applications?
Answer: The legal interaction of threads with the memory in a real computer system defines Java Memory Model. It also describes what behaviors are legal in a multi-threaded code. It can also determine when a Thread can reliably see writes to variables made by other threads. It also defines semantics for volatile, final and synchronize, that makes guarantee of visibility of memory operations across the Threads. In a Memory Barrier which there are two type of memory barrier instructions in JMM - read barriers and write barrier. To make the changes made by other threads visible to the current Thread, the read barrier invalidates the local memory (cache, registers, etc) and then reads the contents from the main memory .To make the changes made by current Thread visible to other Thread, a Write barrier flushes out the contents of the processors local memory to the main memory.
Q20: What happens if you call return statement or System.exit on try or catch block? Will it finally block execute?
Answer: This is a very popular tricky Java question because many programmers think that finally the block always gets executed. This question challenges the concept by putting return statement in try or catch block or calling System.exit from try or catch block. In Java, this finally block will execute even if you put return statement in the try block or catch block finally block won't run even if you call System.exit form to try or catch.