Java Coding Interview Questions and Answers for 7 years experience
-
What is the difference between `==` and `.equals()` in Java?
- Answer: `==` compares references (memory addresses) for objects and primitive values. `.equals()` compares the content of objects. For primitive types, it's essentially the same as `==`. Overriding `.equals()` is crucial for custom classes to ensure meaningful comparison of object contents.
-
Explain the concept of polymorphism in Java. Give an example.
- Answer: Polymorphism allows objects of different classes to be treated as objects of a common type. This is achieved through inheritance and interfaces. A classic example is using an interface like `Animal` with classes `Dog` and `Cat` both implementing a `makeSound()` method. You can then call `makeSound()` on an `Animal` reference, and the correct implementation (barking or meowing) will be executed based on the actual object type.
-
What is the difference between an interface and an abstract class in Java?
- Answer: An interface can only have abstract methods (since Java 8, it can also have default and static methods), while an abstract class can have both abstract and concrete methods. A class can implement multiple interfaces but can only extend one abstract class. Interfaces define what a class *should* do, while abstract classes define a partial implementation and what a class *can* do.
-
Explain the concept of garbage collection in Java.
- Answer: Garbage collection is an automatic memory management process in Java. The Java Virtual Machine (JVM) has a garbage collector that identifies and reclaims memory occupied by objects that are no longer reachable by the program. This prevents memory leaks and simplifies memory management for developers.
-
What are different types of exceptions in Java? Explain with examples.
- Answer: Java exceptions are categorized into checked and unchecked exceptions. Checked exceptions (like `IOException`) must be handled (using `try-catch` or declaring in the method signature) at compile time. Unchecked exceptions (like `NullPointerException` and `ArithmeticException`) are runtime exceptions and don't require explicit handling. Examples: `IOException` during file I/O, `SQLException` during database operations, `NullPointerException` when accessing a null object, `ArithmeticException` when dividing by zero.
-
What is the difference between `HashMap` and `TreeMap` in Java?
- Answer: `HashMap` provides constant-time complexity for basic operations (get, put) on average, but it doesn't guarantee any specific order of elements. `TreeMap` provides logarithmic time complexity for basic operations, but it maintains a sorted order based on keys (using a red-black tree implementation).
-
Explain the concept of concurrency in Java. What are some ways to achieve thread safety?
- Answer: Concurrency allows multiple tasks to run seemingly simultaneously. Thread safety is crucial to prevent data corruption in concurrent environments. Techniques for thread safety include using synchronized methods/blocks, using immutable objects, using concurrent collections (like `ConcurrentHashMap`), and using atomic variables.
-
What is a deadlock in Java? How can you prevent it?
- Answer: A deadlock occurs when two or more threads are blocked indefinitely, waiting for each other to release resources. Prevention strategies include avoiding circular dependencies on resources, acquiring locks in a consistent order, and using timeouts when acquiring locks.
-
Explain the use of Java Streams. Give an example.
- Answer: Java Streams provide a declarative way to process collections of data. They allow for functional-style operations like filtering, mapping, sorting, and reducing data. Example: `List
numbers = ...; int sum = numbers.stream().filter(n -> n > 10).map(n -> n * 2).sum();`
- Answer: Java Streams provide a declarative way to process collections of data. They allow for functional-style operations like filtering, mapping, sorting, and reducing data. Example: `List
Thank you for reading our blog post on 'Java Coding Interview Questions and Answers for 7 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!