Java_Interview_QA
Java_Interview_QA
with Answers
3. Redis:
▲ What is Redis?
Redis (Remote Dictionary Server) is an in-memory data structure store used as a database, cache,
and message broker. It supports data structures such as strings, hashes, lists, sets, and sorted sets
with range queries, bitmaps, and geospatial indexes.
4. Microservices:
▲ Step-by-step implementation of RestTemplate
```
@CircuitBreaker(name = "backendService", fallbackMethod = "fallbackMethod")
public String callExternalService() {
return restTemplate.getForObject("http://external-service/api", String.class);
}
5. Java Concurrency:
▲ Synchronous vs Asynchronous processing
- Synchronous: Tasks are executed sequentially; the caller waits for the response.
- Asynchronous: Tasks run in the background; the caller doesn’t wait for completion, improving
responsiveness.
Using `@Async`:
```
@EnableAsync
@SpringBootApplication
public class AppConfig {}
@Async
public CompletableFuture<String> processAsync() {
return CompletableFuture.completedFuture("Completed");
}
```
Thread locks prevent concurrent modification of shared resources. Real-world example: In a banking
app, prevent multiple withdrawals from the same account at the same time.
```
public class BankAccount {
private final Lock lock = new ReentrantLock();
private int balance;
1. Generate Token:
```
String token = Jwts.builder()
.setSubject(username)
.signWith(SignatureAlgorithm.HS256, secretKey)
.compact();
```
2. Validate Token:
```
String user = Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(token)
.getBody()
.getSubject();
```
9. SQL Indexing:
▲ What is indexing?
Indexing improves query performance by allowing the database to find rows faster without scanning
the entire table.
```
SELECT MAX(salary)
FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee);
```
Docker is a containerization tool that packages applications and dependencies into containers,
ensuring consistency across environments. It simplifies deployments, scaling, and microservices
orchestration.