1. What is the difference between JDK, JRE, and JVM?
- Answer:
- JDK (Java Development Kit) is the toolkit for developing Java applications, including the JRE and development tools.
- JRE (Java Runtime Environment) provides the libraries and environment to run Java applications.
- JVM (Java Virtual Machine) executes Java bytecode on any platform. The JVM is platform-dependent, while bytecode is platform-independent.
2. What are the main features of Java 17?
- Answer: Java 17 introduced features like:
- Sealed classes and interfaces to restrict class hierarchies.
- Enhanced pattern matching for switch statements.
- Stronger encapsulation in the Java API.
- New macOS rendering pipeline for improved performance on macOS.
- Support for Foreign Function & Memory API preview.
3. What is the use of final, finally, and finalize() in Java?
- Answer:
- final: Used to declare constants, prevent method overriding, and inheritance.
- finally: A block in exception handling that executes regardless of exceptions.
- finalize(): A method called by the garbage collector before object destruction, allowing cleanup of resources.
4. Explain the concept of exception handling in Java.
- Answer: Exception handling manages runtime errors, using try, catch, finally, throw, and throws. The try block holds code that may throw an exception, catch handles specific exceptions, and finally executes cleanup code. throw explicitly throws exceptions, while throws indicates exceptions a method may raise.
5. What are functional interfaces and how are they used in Java?
- Answer: A functional interface is an interface with a single abstract method, used in lambda expressions. Common examples are Runnable, Callable, and Java’s built-in interfaces in the java.util.function package (like Consumer, Supplier). Functional interfaces simplify code and are the basis for Java’s functional programming.
6. What is the difference between ArrayList and LinkedList in Java?
- Answer:
- ArrayList: Provides fast access due to index-based retrieval, but slow insertions and deletions since elements need shifting.
- LinkedList: Faster insertion and deletion because it uses a doubly-linked list structure but slower access due to sequential traversal.
7. Explain the significance of the synchronized keyword in Java.
- Answer: synchronized ensures that only one thread can access a block of code or object at a time, preventing race conditions. It can be applied to methods or blocks and is crucial for thread-safe operations.
8. What is the difference between == and .equals() in Java?
- Answer:
- == compares object references, checking if two references point to the same object in memory.
- .equals() compares the content within two objects. It’s typically overridden in classes like String and Integer to provide logical comparison.
9. How does the garbage collection mechanism work in Java?
- Answer: Java’s garbage collector (GC) automatically removes unreferenced objects from memory, optimizing resource usage. The GC uses techniques like Mark and Sweep, Generational Garbage Collection, and G1 (Garbage-First) Collector to efficiently manage memory in the JVM.
10. What is Stream API and how is it used in Java?
- Answer: The Stream API, introduced in Java 8, provides a functional approach to processing sequences of data. It enables operations like filtering, mapping, and reducing, allowing concise and expressive code. Streams can be sequential or parallel for performance optimization, making them valuable in data processing tasks.
These questions cover critical Java topics that interviewers frequently ask, especially for candidates applying for Java developer roles in 2024.