Java_Backend_Interview_Notes
Java_Backend_Interview_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
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
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.
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
Interview Questions:
1. How are microservices different from monoliths?
Java Backend Interview Prep Notes