Core Java (General Questions)

  1. Explain how you would handle a scenario where a method needs to be thread-safe but also efficient.
  2. How would you implement a custom sorting algorithm without using Java’s built-in sort methods?
  3. How would you design an immutable class in Java? What scenarios would require immutability?
  4. Given two unsorted arrays, how would you find the common elements between them?
  5. If you encounter a NullPointerException in production, how would you resolve it?
  6. How would you use Optional to avoid NullPointerException and handle absent values?
  7. Describe how you would optimize memory usage in a Java application.
  8. How would you avoid deadlocks in a multi-threaded application?
  9. Describe a situation where you used volatile and synchronized in your project.
  10. How would you improve the performance of an application with slow database queries?
  11. How would you use Java Streams to manipulate and filter large datasets efficiently?
  12. How would you handle a ClassNotFoundException or NoClassDefFoundError in Java?
  13. What steps would you take to identify and resolve a memory leak in a Java application?
  14. How would you manually trigger garbage collection in Java, and when would it be appropriate to do so?
  15. Describe how you would handle a race condition in a multi-threaded application.
  16. How would you write a program to efficiently read and write a large file in chunks, rather than loading the entire file into memory?
  17. How would you handle an OutOfMemoryError in Java, and how can you prevent such issues from happening?
  18. How would you handle backward compatibility in a serialized Java object when the class structure changes over time?