Monday, November 9, 2009

Java interview questions

The following is the list of some common Java questions asked during interview.I have always found it helpful to make an exhaustive list containing only questions.You can always refer the list when you think its time to brush up.

This list is not exhaustive as of now but I would like to keep this list updated as and when I come across some interesting questions.

  1. What is equals() and What is its significance to java.util.collection package?
  2. What is hashCode() and what is its significance to java.util.collection package?
  3. If equals() is implemented is it must to implement hasCode().Why?
  4. If hashCode() is implemented is it must to implement equals().Why?
  5. Does String class implements equals() and hasCode()?
  6. If you were to write the implementation of String classes equals() what would it be?
  7. Can equals() and hasCode() be modified using Java's Generics?
  8. Why do we have Collections data structure when we can make use of ordinary arrays?
  9. What is the difference between Array and ArrayList?
  10. What is the difference between ArrayList and Vector?
  11. What is the difference between (ArrayList,Vector) and LinkedList?
  12. What are the pros and cons of using an ArrayList?
  13. What are the pros and cons of using a Vector?
  14. What are the pros and cons of using a LinkedList?
  15. What is hashing?
  16. What is the difference between HashSet and LinkedHashSet?
  17. What is TreeSet and how is it different from hash based Set's?
  18. What is the difference between Comparable and Comparator?
  19. What is the significance of Comparable and Comparator to Tree based data structures?
  20. Which data structure will you use when you want to order a list of song's by either the singer or the name of the song.Show me the implementation?
  21. Which data structure will you use when you want a fast search?
  22. What is collision in hash based data structures.
  23. Can you write a simple program to show how hashing works?
  24. Write a code to implement a LinkedList.
  25. Write a code to implement a Stack.Also make sure that when multiple threads are pushing elements onto the stack no elements being pushed are lost.
  26. What are different ways of implementing Threads in java and which would you recommend?
  27. What is object locking?
  28. What is Class locking?
  29. Consider that are 2 synchronized instance (A1() and A2()) and 2 synchronized static (B1 and B2) methods in a class Sample.Now answer the following -
    a] If one thread say T1 is executing A1() of an object obj1 of class Sample, can another thread say T2 execute A2() of obj1?
    b] If one thread say T1 is executing A1() of an object obj1 of class Sample, can another thread say T2 execute B1() of obj1?
    c] If one thread say T1 is executing B1() of an object obj1 of class Sample, can another thread say T2 execute B2() of obj1?
    d] If one thread say T1 is executing A1() of an object obj1 of class Sample, can another thread say T2 execute B2() of another object obj2 of class Sample?
    e] If one thread say T1 is executing B1() of an object obj1 of class Sample, can another thread say T2 execute B2() of another object obj2 of class Sample?
  30. What is final,finalize and finally?
  31. What is the solution to an outofmemory error?
  32. What is Singleton?
  33. What is Factory method pattern?
  34. What is abstract factory pattern?
Algorithm and Data Structures -

1] Write a bubble sort algorithm and its time complexity?
2] What is insertion sort and selection sort ?
3] Which of bubble , selection & insertion sorts time complexity is the best in best case?
4] What is Merge sort and how is its time complexity computed?
5] What is Binary search & its time complexity ?



No comments: