Skip to content

Latest commit

 

History

History
301 lines (156 loc) · 16.7 KB

java-interview.md

File metadata and controls

301 lines (156 loc) · 16.7 KB

Java Interview questions

Collections

Source: https://javarevisited.blogspot.com/2011/11/collection-interview-questions-answers.html

  1. How does HashMap work in Java? (Variations: Why HashMap keys need to be immutable? what is race conditions on HashMap and how HashMap resize in Java?) (answer)

  2. What is the difference between poll() and remove() method of Queue interface?

  3. What is the difference between fail-fast and fail-safe Iterators? (answer)

  4. How do you remove an entry from a Collection? and subsequently what is the difference between the remove() method of Collection and remove() method of Iterator, which one you will use while removing elements during iteration?

  5. What is the difference between Synchronized Collection and Concurrent Collection? (answer)

  6. What is the difference between Iterator and Enumeration? (answer)

  7. How does HashSet is implemented in Java, How does it use Hashing? (answer)

  8. What do you need to do to use a custom object as a key in Collection classes like Map or Set?

  9. The difference between HashMap and Hashtable? (answer)

  10. When do you use ConcurrentHashMap in Java? (answer)

  11. What is the difference between Set and List in Java? (answer)

  12. How do you Sort objects on the collection? What is the different between Comparable and Comparator. (answer)

  13. What is the difference between Vector and ArrayList? (answer)

  14. What is the difference between HashMap and HashSet? (answer)

  15. What is NavigableMap in Java? What is a benefit over Map? (answer)

  16. Which one you will prefer between Array and ArrayList for Storing object and why? (answer). See "Effective Java", Item 28: Prefer lists to arrays.

  17. Can we replace Hashtable with ConcurrentHashMap?

  18. What is CopyOnWriteArrayList, how it is different than ArrayList and Vector? (answer)

  19. Why ListIterator has added() method but Iterator doesn't or Why to add() method is declared in ListIterator and not on Iterator. (answer)

  20. When does ConcurrentModificationException occur on iteration? (answer)

  21. Difference between Set, List and Map Collection classes? (answer)

  22. What is BlockingQueue, how it is different than other collection classes? (answer)

  23. How does LinkedList is implemented in Java, is it a Singly or Doubly linked list? Hint: LinkedList in Java is a doubly linked list.

  24. How do you iterator over Synchronized HashMap, do you need to lock iteration and why?

  25. What is Deque? when do you use it?

Data Structures and Algorithms

Source: https://javarevisited.blogspot.com/2013/03/top-15-data-structures-algorithm-interview-questions-answers-java-programming.html

Concurrency

Source: https://javarevisited.blogspot.com/2014/07/top-50-java-multithreading-interview-questions-answers.html#axzz6UiZ28HdV

  1. What is Thread in Java? (answer)

  2. What is the difference between Thread and Process in Java? (answer)

  3. How do you implement Thread in Java? (answer)

  4. When to use Runnable vs Thread in Java? (answer)

  5. What is the difference between start() and run() method of Thread class? (answer)

  6. What is the difference between Runnable and Callable in Java? (answer)

  7. What is the difference between CyclicBarrier and CountDownLatch in Java? (answer)

  8. What is Java Memory model?

  9. What is volatile variable in Java? (answer)

  10. What is thread-safety? is Vector a thread-safe class? (answer)

  11. What is race condition in Java? Given one example? (answer)

  12. How to stop a thread in Java?(answer)

  13. What happens when an Exception occurs in a thread?

  14. How do you share data between two thread in Java? (Producer-consumer pattern)

  15. What is the difference between notify and notifyAll in Java? (answer)

  16. Why wait, notify and notifyAll are not inside Thread class? ([answer](What is ThreadLocal variable in Java?))

  17. What is ThreadLocal variable in Java? (answer)

  18. What is FutureTask in Java?

  19. What is the difference between the interrupted() and isInterrupted() method in Java?

  20. Why wait and notify method are called from synchronized block? (answer)

  21. Why should you check condition for waiting in a loop? (answer)

  22. What is the difference between synchronized and concurrent collection in Java?

  23. What is the difference between Stack and Heap in Java? (answer)

  24. What is thread pool? Why should you thread pool in Java?

  25. Write code to solve Producer-Consumer problem in Java? (see)

  26. How do you avoid deadlock in Java? Write Code? (answer)

  27. What is the difference between livelock and deadlock in Java?

  28. How do you check if a Thread holds a lock or not?

  29. How do you take thread dump in Java?

  30. Which JVM parameter is used to control stack size of a thread?

  31. What is the difference between synchronized and ReentrantLock in Java? (answer)

  32. There are three threads T1, T2, and T3? How do you ensure sequence T1, T2, T3 in Java? (answer)

  33. What does yield method of Thread class do? (answer)

  34. What is the concurrency level of ConcurrentHashMap in Java? (answer)

  35. What is Semaphore in Java?

  36. What happens if you submit a task when the queue of the thread pool is already filled?

  37. What is the difference between the submit() and execute() method thread pool in Java? (answer)

  38. What is blocking method in Java? (answer)

  39. How to create an Immutable object in Java? (answer)

  40. What is ReadWriteLock in Java?

  41. What is busy spin in multi-threading?

  42. What is the difference between the volatile and atomic variable in Java?

  43. What happens if a thread throws an Exception inside synchronized block?

  44. How to create thread-safe Singleton in Java?

  45. List down 3 multi-threading best practice you follow?

  46. How do you force to start a Thread in Java?

  47. What is the fork-join framework in Java? (answer)

  48. What is the difference between calling wait() and sleep() method in Java multi-threading? (answer)

Phone interview

Source: https://javarevisited.blogspot.com/2018/07/top-30-java-phone-interview-questions.html

  1. Why is String immutable in Java? (answer)

  2. Can abstract class have a constructor in Java?

  3. Which two methods are overridden by an Object, intended to be used as a key in HashMap? (answer)

  4. What is the difference between wait and sleep in Java? (answer)

  5. Difference between List and Set in Java? (easy)

  6. Difference between List and Set in Java? (answer)

  7. Which data type you should use to represent currency in Java? (easy)

  8. When to use abstract class and interface in Java? (easy)

  9. Difference between Hashtable and HashMap in Java? (easy)

  10. What is the difference between ArrayList and LinkedList in Java? (easy)

  11. What is the difference between Overloading and Overriding in Java? (Compile time vs Runtime polymorphism. See this and this)

  12. Difference between checked and unchecked exception in Java?

  13. Difference between checked and unchecked exception in Java? (answer)

  14. Does Java array is an instance of Object?

  15. Does List<Number> can hold Integers? (easy)

  16. Can we pass ArrayList<Number> to a method which accepts List<Number> in Java?

  17. Can we pass ArrayList<Integer> to a method which accepts List<Number>?

  18. What is a volatile variable in Java?

  19. What is the difference between CountDownLatch and CyclicBarrier in Java?

  20. Does BlockingQueue is thread-safe in Java?

  21. Why wait and notify method should be called in a loop?

  22. What is the difference between "ABC".equals(unknown string) and unknown.equals("ABC")? (NullPointerException)

  23. What is a marker or tag interface in Java? (answer)

  24. Can Enum types implement interface in Java? (Yes)

  25. Can Enum extend a class in Java? (No)

  26. How to prevent your class from being subclassed? (answer)

  27. Can we override a static method in Java? Compilation error? (answer)

  28. Which design pattern have you used recently?

  29. What is the difference between StringBuffer and StringBuilder in Java?

Generics

Source: https://javarevisited.blogspot.com/2020/04/50-java-collection-and-generics-interview-questions-answers.html, https://javarevisited.blogspot.com/2020/04/50-java-collection-and-generics-interview-questions-answers.html

  1. What is Generics in Java ? What are advantages of using Generics?

  2. How Generics works in Java ? What is type erasure?

  3. What is Bounded and Unbounded wildcards in Generics? What is the difference between bounded and unbounded wildcards in Java generics? What is difference between List<? extends T> and List <? super T>?

  4. Write a program to implement LRU cache using Generics ?

  5. Can you pass List<String> to a method which accepts List<Object>?

  6. Can we use Generics with Array?

  7. Difference between List<Object> and raw type List in Java?

  8. Difference between List<?> and List<Object> in Java?

  9. Difference between List<String> and raw type List?

  10. What is an "unchecked" warning?

  11. What is a bridge method?

  12. How can I avoid "unchecked cast" warnings?

  13. What is the "diamond" operator?

  14. Difference between ArrayList and ArrayList<?> in Java? (Raw type vs Wildcard)

Frequently Asked Questions

Source: https://www.java67.com/2014/07/21-frequently-asked-java-interview-questions-answers.html

Other Core Java Questions

  1. What are the types of Java Threads? (See)

  2. What is a class loader?

  3. What is volatile "happens-before" relationship - "A happens-before relationship is a guarantee that memory written to by statement A is visible to statement B, that is, that statement A completes its write before statement B starts its read."

  4. Why multiple inheritance is not supported in Java? (answer)

  5. Does Java Pass by Value or Pass by Reference? (answer)

  6. Can You Run Java Program Without a Main Method? (answer)

  7. Why main method is public static and void in Java? (answer)

  8. Why character array is better than String for Storing password in Java? (See this and this answer)

  9. Why Default or No Argument Constructor is Important in Java Class? (answer)

  10. Can we have a static & final method? (Yes)

  11. Can we have an abstract & final method? (No)

  12. What are the implications of declaring a constructor private?

  13. Can there be a private/constructor in an abstract class? (see)

  14. new String(“str”); how many objects will be created? (see)

  15. Can a constructor be final? (No, see)

  16. How to sort values in HashMap? (answer)