180 likes | 441 Views
Crack your Java interview by knowing all the questions that your interviewer may ask. Brush up your knowledge with this superb set of Java interview questions
E N D
Java Interview Q & A By www.bestonlinetrainers.com
Q11: Differentiate between a constructor and a method? Answer: • A member function of a class that is used to create objects of that class is called a Constructor is. Its name is same as the class itself, has no return type. It is invoked using the new operator. An ordinary member function of a class is called Method. It has its own name and a return type (which may be void).It is invoked using the dot operator. Learn Java. Ask for a free demowww.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. Learn Java. Ask for a free demowww.bestonlinetrainers.com
Q13:When is HashCode and equals() overridded ? • Answer: To do equality check or use object as a key in HashMap, HashCode and equals() are overridden.
Q14:What is difference between StringBuffer and StringBuilder in Java? • Answer: If the HashCode() is not overridded , the object will not be recovered from the HashMap if used as key.
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 what’s 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 thread-safety 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 processor’s 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.
Want to learn Java? Visit : http://www.bestonlinetrainers.com/programming/java-training.html Email : info@bestonlinetrainers.com Phone : USA: +(1) 6783896789 UK: +(44)- 2032393637 India: +(91) 9246449191 Ask for a free demo TODAY : http://www.bestonlinetrainers.com/demo/index.html