Java Backend Interview Prep Notes
1. Java Multithreading
- Thread Lifecycle: NEW -> RUNNABLE -> RUNNING -> BLOCKED -> TERMINATED
- Ways to create threads: Extending Thread class or implementing Runnable interface
- Synchronization: Use `synchronized` keyword to control access to critical sections
- Volatile: Ensures visibility of changes across threads
- wait(), notify(), notifyAll(): Used for thread communication
- Concurrency utilities: ExecutorService, Callable, Future, CountDownLatch, ReentrantLock
Interview Questions:
1. Difference between Thread and Runnable?
- Thread is a class; Runnable is an interface. Runnable is preferred for flexibility.
2. What is the purpose of volatile?
- Prevents caching of variable so all threads see latest value.
3. How does synchronized work?
- It locks a block or method so only one thread can access it at a time.
4. What is deadlock?
- When two or more threads wait for each other to release resources, causing a standstill.
2. Java 8
- Lambda Expressions: `() -> {}` for functional-style code
- Functional Interfaces: Interface with a single abstract method (`@FunctionalInterface`)
- Streams API: Allows functional-style operations like map, filter, reduce
- Optional: Avoids null pointer exceptions
- Default and Static methods in Interfaces
Interview Questions:
1. What is a functional interface?
- An interface with a single abstract method (e.g., Runnable, Comparator).
2. Difference between map and flatMap?
- map transforms each element, flatMap flattens the result.
3. How does Optional prevent NullPointerException?
- Provides a wrapper with methods like isPresent(), orElse().
3. Spring Boot
- @SpringBootApplication: Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan
- @Component, @Service, @Repository: Bean stereotypes
Java Backend Interview Prep Notes
- application.properties: Central config for DB, ports, logging
- Dependency Injection via @Autowired
- @ControllerAdvice and @ExceptionHandler for global exception handling
Interview Questions:
1. How is Spring Boot different from Spring?
- It reduces boilerplate and config with auto-configuration and starters.
2. How to create REST endpoints in Spring Boot?
- Use @RestController, @GetMapping, @PostMapping, etc.
3. What is the purpose of application.properties?
- Configure database, server port, logging, etc.
4. RESTful Web Services
- HTTP methods: GET, POST, PUT, DELETE, PATCH
- Status codes: 200 OK, 201 Created, 400 Bad Request, 401 Unauthorized, 404 Not Found, 500 Server Error
- @RestController: Combines @Controller and @ResponseBody
- @RequestParam, @PathVariable for handling input
- Swagger: API documentation tool
Interview Questions:
1. Difference between @RequestParam and @PathVariable?
- RequestParam is for query parameters; PathVariable is for URL segments.
2. How to validate a request body?
- Use @Valid and Bean Validation API (e.g., @NotNull, @Size).
3. How to handle exceptions in REST API?
- Use @ControllerAdvice and @ExceptionHandler.
5. Microservices
- Architecture: Decentralized, independently deployable services
- Communication: REST, gRPC, Messaging (Kafka, RabbitMQ)
- Service Discovery: Eureka
- API Gateway: Spring Cloud Gateway
- Resilience: Circuit Breaker (Resilience4j), Retry
- Centralized Config: Spring Cloud Config
- Monitoring: Actuator, Prometheus
Interview Questions:
1. How are microservices different from monoliths?
Java Backend Interview Prep Notes
- Microservices are loosely coupled and independently deployable.
2. What is service discovery?
- Services register themselves and discover others via registry like Eureka.
3. How do you handle failure between services?
- Retry, Circuit Breaker, fallback methods.
4. How do microservices communicate?
- REST, message queues, gRPC.