50 Intermediate Java Interview Questions
1. What is the difference between JDK, JRE, and JVM?
Ans: JDK is a software development kit for Java, JRE is a runtime environment, and JVM is the virtual machine executing Java bytecode.
2. Explain the concept of Object-Oriented Programming in Java.
Ans: OOP is a programming paradigm based on objects, encapsulating data and behavior.
3. What is the difference between a class and an object in Java?
Ans: A class is a blueprint for creating objects, while an object is an instance of a class.
4. Explain the concept of polymorphism in Java with an example.
Ans: Polymorphism allows objects of different types to be treated as if they were of the same type. It's achieved through method overloading and overriding.
5. What is the difference between a static and non-static method?
Ans: Static methods belong to a class, while non-static methods belong to an object.
6. Explain the concept of garbage collection in Java.
Ans: Garbage collection is the automatic process of reclaiming memory no longer in use.
7. What is the difference between an array and an ArrayList?
Ans: An array is a fixed-size collection of elements, while an ArrayList is a dynamic, resizable collection.
8. Explain the concept of a linked list.
Ans: A linked list is a linear data structure where elements are linked together using pointers.
9. What is a HashMap, and how does it work?
Ans: A HashMap is a data structure storing key-value pairs, using a hash function to map keys to indices and handling collisions.
10. Write a Java program to find the factorial of a number using recursion.
Ans: // Provide a code snippet for factorial using recursion
11. Explain the time and space complexity of a binary search algorithm.
Ans: Binary search has a time complexity of O(log n) and space complexity of O(1).
12. What is the difference between String, StringBuilder, and StringBuffer?
Ans: String is immutable, StringBuilder is mutable and not thread-safe, while StringBuffer is mutable and thread-safe.
13. Explain the concept of exception handling in Java.
Ans: Exception handling is a mechanism to manage errors or exceptions during program execution.
14. What is the difference between checked and unchecked exceptions?
Ans: Checked exceptions must be handled or declared, while unchecked exceptions are runtime exceptions.
15. Explain the use of the finally block in Java.
Ans: The finally block ensures code execution regardless of whether an exception is thrown or not.
16. What are lambda expressions in Java 8?
Ans: Lambda expressions are concise syntax for representing anonymous functions.
17. Explain the concept of functional interfaces.
Ans: A functional interface is an interface with exactly one abstract method.
18. What is the Stream API in Java 8?
Ans: The Stream API provides a declarative way to process collections of data.
19. Explain the difference between thread and process.
Ans: A thread is a lightweight subprocess of a process, sharing the same memory space, while a process is an independent program with its own memory space.
20. What is synchronization in Java?
Ans: Synchronization is a mechanism to control access to shared resources among multiple threads.
21. Explain the concept of deadlock.
Ans: Deadlock occurs when two or more threads are blocked, each waiting for the other to release a resource.
22. What is dependency injection?
Ans: Dependency injection is a design pattern where objects receive their dependencies from external sources.
23. Explain the core components of Spring Framework.
Ans: Spring Framework consists of Core Container, AOP, Data Access, Web, and Test modules.
24. What are the different types of beans in Spring?
Ans: Spring beans can be singletons, prototypes, request, session, or application scoped.
25. Describe a challenging Java project you worked on and the problem you solved.
Ans: This open-ended question allows the candidate to showcase their problem-solving skills and technical expertise.
26. Explain the difference between shallow copy and deep copy in Java.
Ans: A shallow copy creates a new object but shares the same references to the original object's data, while a deep copy creates a new object with independent copies of the original object's data.
27. What is the difference between an interface and an abstract class?
Ans: An interface defines a contract with method signatures but no implementation, while an abstract class can have both abstract and concrete methods.
28. What is the difference between a HashSet and a TreeSet?
Ans: HashSet uses a hash table for storage, allowing for faster lookups but unsorted elements, while TreeSet uses a tree structure for storage, ensuring sorted elements.
29. Explain the concept of serialization and deserialization.
Ans: Serialization is the process of converting an object into a byte stream, while deserialization is the reverse process of converting a byte stream back into an object.
30. What is the difference between equals() and == operator in Java?
Ans: The equals() method compares the contents of two objects, while the == operator compares object references.
31. Explain the concept of generics in Java.
Ans: Generics allow you to create classes and methods that work with different types of objects without compromising type safety.
32. What is the difference between early binding and late binding?
Ans: Early binding occurs at compile time, while late binding occurs at runtime.
33. What is the use of the transient keyword in Java?
Ans: The transient keyword prevents a field from being serialized.
34. Explain the concept of inner classes in Java.
Ans: Inner classes are classes defined within another class.
35. What is the difference between a local inner class and a static nested class?
Ans: A local inner class can access local variables of the enclosing method, while a static nested class cannot.
36. Explain the concept of method overloading and overriding.
Ans: Method overloading involves having multiple methods with the same name but different parameters within a class, while method overriding involves defining a method in a subclass with the same name and parameters as a method in the parent class.
37. What is the difference between a final, finally, and finalize() keyword?
Ans: final is used for declaring constants, finally is a block of code that always executes, and finalize() is a method called by the garbage collector before an object is reclaimed.
38. Explain the concept of a thread-safe class.
Ans: A thread-safe class can be used safely by multiple threads without causing data corruption.
39. What are the different ways to achieve thread synchronization in Java?
Ans: Synchronization can be achieved using synchronized keyword, locks, and atomic variables.
40. Explain the producer-consumer problem and its solution.
Ans: The producer-consumer problem involves multiple producer threads producing data and multiple consumer threads consuming data. It can be solved using techniques like blocking queues, semaphores, or monitors.
41. What is the difference between wait(), notify(), and notifyAll() methods?
Ans: wait() releases the lock and puts the thread to sleep, notify() wakes up a single waiting thread, and notifyAll() wakes up all waiting threads.
42. Explain the concept of deadlock.
Ans: Deadlock occurs when two or more threads are blocked, each waiting for the other to release a resource.
43. What is the use of the volatile keyword in Java?
Ans: The volatile keyword ensures that changes to a variable are visible to all threads.
44. Explain the concept of lazy loading.
Ans: Lazy loading is a technique where objects are not initialized until they are actually needed.
45. What is the difference between eager loading and lazy loading?
Ans: Eager loading loads all related data at once, while lazy loading loads data only when it's required.
46. Explain the concept of dependency injection.
Ans: Dependency injection is a design pattern where objects receive their dependencies from external sources.
47. What are the different types of dependency injection?
Ans: Constructor injection, setter injection, and field injection.
48. What is the role of the IoC container in Spring?
Ans: The IoC container manages the creation and lifecycle of beans.
49. Explain the concept of AOP in Spring.
Ans: AOP allows you to add cross-cutting concerns like logging, security, and transaction management without modifying the core business logic.
50. What is the difference between Hibernate and JPA?
Ans: Hibernate is an ORM implementation, while JPA is a specification for ORM.