Showing posts with label Java Interview Questions. Show all posts
Showing posts with label Java Interview Questions. Show all posts

Sunday, January 31, 2010

Java Collections API Interview Questions

Question1:You need to insert huge amount of objects and randomly delete them one by one. Which Collection data structure is best pet?


Answer:
LinkedList.

Question2:What goes wrong if the HashMap key has same hashCode value?


Answer:
It leads to ‘Collision’ wherein all the values are stored in same bucket. Hence, the searching time increases quad radically.

Question:3 If hashCode() method is overridden but equals() is not, for the class ‘A’, then what may go wrong if you use this class as a key in HashMap?


Answer:


Question:4. How will you remove duplicate element from a List?


Answer:
Add the List elements to Set. Duplicates will be removed.

Question:5. How will you synchronize a Collection class dynamically?


Answer:
Use the utility method
java.util.Collections.synchronizedCollection(Collection c)

Courtsey:
http://excusemeworld.com

Java Threads Interview Questions

Question 1:. Write a Java program to create three threads namely A, B, C and make them run one after another. (C has to run after B completes, B has to run after A completes).



Answer:-Many ways to do it. I prefer join(). When a thrad A calls b.join(), then A runs only after thread b completes. So, for this problem, C has to call b.join() and B has to call a.join()


Question 2:What happens when a thread calls notify() but no thread(s) is waiting?


Answer:-Actually…, nothing the notify() call simply returns.


Question 3:How will you declare a timer variable that will be accessed by multiple threads very frequently?


Answer:-Declare the variable as volatile. Every thread caches a copy of instance variable, work with local copy, and then sync it with master copy. But, threads do not cache a copy of volatile instance variable.


Question 4:How will you synchronize static variables?


Answer:-
Obtain class level lock.
synchronized( obj.getClass()) {……..}

Question 5:What happens when threads are waiting, but never being notified?

Java Interview Questions

Question 1: Give the use and an example of the scenario where you would use serialVersionUID in a your Java class?


Question 2: Design a Employee class with name, empId, age and address fields.
Any two Employee objects are compared based on their age.
The empId is the unique id for Employee class. The Employee class can be used in Collections and HashMap.


Question 3: Explain what goes in a HashMap or Hashtable, when you store the objects with same hashcode values. Explain your answer in very depth.


Question 4: In JVM Memory, where the private, public, protected, static, String literal and object references are stored?


Question 5: How will you store your exception stack trace in a file? (you should not redirect the console output)


Question 6: What is the use of Weak, Soft, Phantom references in Garbage Collection of java? Give scenarios.


Question 7: How will you keep an object alive for ever in the JVM memory? (trick: use finalize() method)


Question8: What is the difference between JiBX and JAXB?



Answer:
JiBX can provide significant performance improvements over JAXB as it does not use reflection, but instead does byte code generation to optimize databinding.