1. What is the Java programming language? #
Answer:
Java is a high-level, object-oriented, platform-independent programming language. It follows the WORA (Write Once, Run Anywhere) principle, meaning compiled Java code can run on any platform with a compatible JVM.
2. What are the key features of Java? #
Answer:
- Object-Oriented: Based on concepts like classes and objects.
- Platform-Independent: Compiled bytecode runs on any platform.
- Secure: Provides built-in security features (e.g., bytecode verification, access control).
- Robust: Includes features like exception handling and memory management (via Garbage Collection).
- Multithreaded: Supports concurrent execution of threads.
3. What is the difference between JDK, JRE, and JVM? #
Answer:
- JDK (Java Development Kit): A development environment containing tools like the compiler and debugger.
- JRE (Java Runtime Environment): Provides the libraries and JVM to run Java applications.
- JVM (Java Virtual Machine): Executes bytecode and provides a runtime environment.
4. What are the access modifiers in Java? #
Answer:
- public: Accessible everywhere.
- protected: Accessible within the same package and subclasses.
- default (no modifier): Accessible within the same package.
- private: Accessible only within the declared class.
5. What is the difference between == and .equals()? #
Answer:
==compares object references (memory locations)..equals()compares object contents (logical equality).
6. What is a Java Constructor? #
Answer:
A constructor is a special method used to initialize objects. It has the same name as the class and no return type.
Example:
public class Example {
Example() {
System.out.println("Constructor called");
}
}
7. What is the difference between final, finally, and finalize()? #
Answer:
- final: A keyword to declare constants or prevent inheritance/method overriding.
- finally: A block in exception handling that executes regardless of whether an exception is thrown.
- finalize(): A method called by the Garbage Collector before an object is destroyed.
8. What is the difference between Abstract Class and Interface? #
Answer:
- Abstract Class: Can have abstract and concrete methods, allows inheritance, and supports fields with access modifiers.
- Interface: Contains only abstract methods (before Java 8) and constants, supports multiple inheritance, and cannot have instance fields.
9. What are static methods and variables? #
Answer:
- Static methods: Belong to the class and not instances. They can be called without creating an object.
- Static variables: Shared across all instances of a class.
Example:
class Example {
static int count = 0;
static void showCount() {
System.out.println("Count: " + count);
}
}
10. What is the difference between Array and ArrayList? #
Answer:
- Array: Fixed size, can store primitive types and objects.
- ArrayList: Dynamic size, part of the Java Collections framework, stores objects only.
11. What is method overloading and overriding? #
Answer:
- Overloading: Defining methods with the same name but different parameters.
- Overriding: Providing a new implementation of a method in a subclass.
12. What is a try-catch block? #
Answer:
A try-catch block handles exceptions in Java.
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
}
13. What are checked and unchecked exceptions? #
Answer:
- Checked Exceptions: Must be handled or declared in the method signature (e.g., IOException).
- Unchecked Exceptions: Occur at runtime and do not need to be explicitly handled (e.g., NullPointerException).
14. What is the difference between String, StringBuilder, and StringBuffer? #
Answer:
- String: Immutable and stored in the string pool.
- StringBuilder: Mutable, non-thread-safe.
- StringBuffer: Mutable, thread-safe due to synchronized methods.
15. What is Garbage Collection in Java? #
Answer:
Garbage Collection automatically deallocates unused memory. The JVM identifies and removes objects no longer referenced. This improves memory efficiency.
16. What is the synchronized keyword? #
Answer:
The synchronized keyword prevents multiple threads from accessing a method or block simultaneously, ensuring thread safety.
Example:
synchronized void increment() {
count++;
}
17. What are Java Streams? #
Answer:
Streams are used for functional-style operations on collections. They support methods like filter(), map(), and reduce().
Example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
18. What is the purpose of the volatile keyword? #
Answer:
The volatile keyword ensures that a variable's value is always read from and written to main memory, not a thread's cache, ensuring visibility in a multithreaded environment.
19. What is the Java Collections Framework? #
Answer:
The Java Collections Framework is a set of classes and interfaces for storing and manipulating data structures like lists, sets, maps, and queues.
20. What is the difference between HashMap and ConcurrentHashMap? #
Answer:
- HashMap: Not thread-safe, may cause issues in concurrent environments.
- ConcurrentHashMap: Thread-safe, allows concurrent access with segment-level locking.