0% found this document useful (0 votes)
75 views3 pages

Java Backend Interview Prep Guide

The document provides comprehensive notes for Java backend interview preparation, covering key topics such as Java multithreading, Java 8 features, Spring Boot, RESTful web services, and microservices. Each section includes essential concepts, interview questions, and answers to help candidates understand the material. It emphasizes important Java features, design patterns, and best practices relevant to backend development.

Uploaded by

Rachit Garg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views3 pages

Java Backend Interview Prep Guide

The document provides comprehensive notes for Java backend interview preparation, covering key topics such as Java multithreading, Java 8 features, Spring Boot, RESTful web services, and microservices. Each section includes essential concepts, interview questions, and answers to help candidates understand the material. It emphasizes important Java features, design patterns, and best practices relevant to backend development.

Uploaded by

Rachit Garg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

- [Link]: 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 [Link]?
- 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.

Common questions

Powered by AI

The @SpringBootApplication annotation simplifies the configuration process by combining three commonly used annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan . It configures the application context by enabling auto-configuration and scanning for components, thus reducing the need for manual XML configuration and boilerplate code . This integration allows developers to build and run Spring-based applications with minimal setup, streamlining the development process and enabling quick deployment.

@ControllerAdvice is used to define a global exception handler for multiple controllers, promoting centralized error handling across the Spring Boot application . @ExceptionHandler annotations are used within a class annotated with @ControllerAdvice to define specific methods for handling particular exception types . This approach not only contributes to application stability by handling exceptions in a predictable and uniform manner but also enhances code maintainability by decoupling exception handling logic from business logic.

Spring Cloud Config provides centralized configuration management, allowing for externalization of configuration properties into a central repository . This benefits development and deployment by ensuring consistent configuration across services, simplifying management, and allowing changes to configurations without redeploying applications . It enhances flexibility and maintainability by enabling different configurations for different environments and easy updates, contributing to more efficient and agile operation of distributed systems.

In Java Streams API, 'map' is used to transform each element of a stream into another form as defined by a function, producing a single value per input element . 'flatMap', on the other hand, applies a function that returns another stream for each element, and then flattens the resulting streams into a single stream . The key difference is that flatMap also performs flattening in addition to transformation, thus dealing with nested collections more effectively than map.

ExecutorService allows for a pool of threads to be reused, which reduces the overhead of creating new threads for each task . Callable is similar to Runnable but can return a result and throw a checked exception, providing more flexibility in tasks execution . Future represents the result of an asynchronous computation, allowing the program to retrieve the result of a task when it’s ready, thus providing control over asynchronous computations . Together, these utilities enhance the management and efficiency of concurrent processes.

Service discovery in microservices involves services registering themselves and discovering other services via a central registry, typically implemented using tools like Eureka . It enables dynamic resolution of service locations, automatically updates available services, and manages its service instances, which is crucial for maintaining scalability and reliability . This is particularly important in highly dynamic environments where instances of services appear and disappear frequently, ensuring that requests are always directed to available services.

The stages of a thread lifecycle in Java are NEW, RUNNABLE, RUNNING, BLOCKED, and TERMINATED . The transition between these stages can be controlled by mechanisms such as synchronization and volatile. Synchronization locks a method or block to ensure only one thread can access it at a time, preventing race conditions . Volatile ensures that thread reads the latest value of a variable by preventing caching, thereby maintaining visibility across threads . These mechanisms help manage the safe execution and state transitions in multithreading.

Circuit breakers, such as those offered by Resilience4j, are critical in a microservice architecture as they help improve system resilience by preventing overload and cascading failures . They provide mechanisms to open and thereby short-circuit requests to failing services, allowing time for recovery and preventing the wastage of resources on repeated failed requests . This approach contributes significantly to stability by ensuring that one service's failure does not impact others, maintaining system performance and reliability.

Optional in Java wraps a value which might be null, thereby avoiding NullPointerException by providing methods like isPresent(), orElse(), and orElseGet() which allow developers to define a default value or action when the value is absent . It supports key operations such as checking presence (isPresent()), providing a default value (orElse()), executing alternative actions (ifPresent()), and handling absence gracefully without null checks .

RESTful web services use HTTP status codes to indicate the result of the client's request, such as 200 OK for success, 201 Created for resource creation, 400 Bad Request for client errors, and 500 Server Error for server-side issues . Understanding these codes is crucial for API development because they provide standardized communication of operation outcomes to clients, allowing for appropriate error handling and responses. This not only aids in debugging and monitoring but also ensures predictable behaviors in client-server interactions.

You might also like