All Interview Imp
All Interview Imp
1. **Finalize Method**:
- The `finalize()` method in Java is called by the Garbage Collector (GC) on an object before the
object is garbage collected. It allows cleanup before memory is reclaimed.
2. **Invoke GC**:
- You can suggest the JVM to run the garbage collector using `System.gc()` or
`Runtime.getRuntime().gc()`, though it's not guaranteed to run immediately.
3. **Types of GC**:
- **Serial GC**: A single-threaded GC suitable for small applications.
- **Parallel GC**: Uses multiple threads for GC, useful for multi-core systems.
- **CMS (Concurrent Mark-Sweep) GC**: Targets low-pause applications by performing GC
concurrently with the application.
- **G1 (Garbage First) GC**: Designed for multi-core machines with large memory, focuses on low-
pause and high-throughput.
5. **G1 GC**:
- Garbage First (G1) GC divides the heap into regions and prioritizes reclaiming the most garbage-
filled regions first, balancing pause times and throughput.
### Database
2. **Clustered**:
- **Clustered Index**: A type of index where the table records are physically ordered based on the
indexed column(s).
4. **Data Modeling**:
- **Data Modeling**: The process of creating a data model for the data to be stored in a database.
It includes conceptual, logical, and physical models.
5. **Data Sharding**:
- **Data Sharding**: The practice of splitting a large database into smaller, faster, more easily
managed parts called shards.
6. **Query for Second Highest Salary Employee**:
```sql
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
```
### MultiThreading
1. **Runnable vs Callable**:
- **Runnable**: Doesn't return a result or throw a checked exception.
- **Callable**: Can return a result and throw a checked exception.
2. **Executor Service**:
- **Executor Service**: A higher-level replacement for working with threads directly. It provides
methods to manage the lifecycle of threads.
6. **Reentrant Lock**:
- A lock that allows the thread holding the lock to reacquire it without getting blocked.
7. **Thread Local**:
- Provides thread-local variables, ensuring that each thread has its own independent instance of the
variable.
8. **Atomic Variable**:
- Variables that are updated atomically, ensuring thread-safe operations on single variables without
locking.
### Collection
7. **CopyOnWriteArrayList**:
- A thread-safe variant of `ArrayList` where all mutative operations create a new copy of the
underlying array.
5. **Comparable vs Comparator**:
- **Comparable**: Used to define the natural ordering of objects (e.g., `compareTo()`).
- **Comparator**: Used for custom sorting, allows sorting by multiple criteria.
- **JSON Library**: Used for parsing and generating JSON data in Java. Popular libraries include
Jackson and Gson.
- **XML Library**: Used for processing XML documents in Java. Examples include JAXB and DOM4J.