0% found this document useful (0 votes)
16 views6 pages

Java Microservices and Spring Boot Interview

The document contains a series of Java and Spring Boot interview questions and answers covering various topics such as Java 8 features, Spring Boot application creation, exception handling, microservices, and performance optimization. Key concepts include default methods in interfaces, try-with-resources, the difference between Set and List, and how to secure REST endpoints. It also discusses containerization, deployment in AWS, and handling performance issues in inter-service calls.

Uploaded by

kumariitian011
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)
16 views6 pages

Java Microservices and Spring Boot Interview

The document contains a series of Java and Spring Boot interview questions and answers covering various topics such as Java 8 features, Spring Boot application creation, exception handling, microservices, and performance optimization. Key concepts include default methods in interfaces, try-with-resources, the difference between Set and List, and how to secure REST endpoints. It also discusses containerization, deployment in AWS, and handling performance issues in inter-service calls.

Uploaded by

kumariitian011
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 & Spring Boot Interview Q&A

1. What is the purpose of default and static methods in Java 8 interfaces?

a
 Default methods allow adding new methods to interfaces without breaking

al
existing implementations.
 They provide backward compatibility. Static methods help group utility methods
inside interfaces.
 They reduce the need for utility classes. Both enhance interface flexibility.
ew
2. Explain try-with-resources in Java.

 Try-with-resources automatically closes resources after use.


 It ensures no resource leaks like file or DB connections. Any class implementing
AutoCloseable can be used.
 It avoids verbose finally blocks. Introduced in Java 7 for cleaner code.
d
3. Difference between Set and List?
Ko

 Set does not allow duplicates, List allows them.


 Set is unordered, List maintains insertion order.
 Set operations are faster for search.
 List allows random access using index.
 Set is best for uniqueness, List for ordered data.
4. How do you avoid duplicate Employee objects in a Set?

 Override equals() and hashCode() in Employee class.


 This ensures logical equality comparison.
 HashSet checks duplicates using hashCode + equals.
 Without it, objects with same data may still duplicate. Thus uniqueness is
enforced.

a
al
5. When do you use Consumer and Supplier?

 Consumer accepts a value but returns nothing.


 Supplier provides a value but accepts nothing.


ew
Consumer is used for operations like printing.
Supplier is used for lazy object creation.
 Both are functional interfaces in Java 8.

6. How do you create a Spring Boot app?

 Use Spring Initializr or [Link] to generate a project.


d
 Add dependencies like Web, JPA, or Security.
 Use @SpringBootApplication annotation in main class.
 Run the app using [Link]().
 Access REST APIs at defined endpoints.
Ko

7. How to group Employees by department using Streams?

 Use [Link]() in Streams.


 Pass Employee::getDepartment as classifier.
 It returns a Map<Department, List<Employee>>.
 Allows aggregation per department.
 Efficient for classification tasks.
8. What happens if two beans are interdependent in Spring Boot?

 It creates circular dependency.


 Spring fails to start the context.
 Can be resolved by @Lazy annotation.
 Or refactor dependencies to remove the loop.
 Constructor injection is most prone.

a
9. How do you call multiple microservices from one API?

al
 Use an API Gateway or Aggregator service.
 Gateway routes requests to multiple services.
 Aggregator combines results and sends one response.
 Feign clients or RestTemplate can be used. This reduces client complexity.
ew
10. How do you call secured APIs with token?

 Obtain token from authentication server.


 Pass token in Authorization header.
 Use RestTemplate or WebClient to call APIs.
 Bearer token format is standard.
d
 Token validity must be managed.

11. How to secure a REST endpoint in Spring Boot?


Ko

 Use Spring Security dependency.


 Configure HttpSecurity with antMatchers.
 Use roles and authorities for access.
 JWT/OAuth2 tokens can be integrated.
 Endpoints get restricted by config.
12. How do you cache DB query results at startup?

 Use @Cacheable annotation in Spring.


 Load data on Application startup event.
 Store data in in-memory caches like Redis or EhCache.
 Reduces repeated DB calls. Improves application performance.

a
13. How to handle exceptions globally in Spring Boot?

 Use @ControllerAdvice with @ExceptionHandler.

al
 This centralizes exception handling.
 Custom error responses can be returned.
 Removes boilerplate try-catch. Ensures consistent error format.

14. How do you implement role-based access in APIs?


ew
 Use Spring Security with roles.
 Assign roles to users in DB. Restrict APIs using hasRole() in config.
 Use @PreAuthorize on methods if needed.
 Provides fine-grained access control.
d
15. How to investigate sudden OutOfMemoryError in production?

 Enable heap dumps on OOM.


Ko

 Analyze dump using tools like Eclipse MAT. Check GC logs for memory leaks.
 Profile memory with monitoring tools. Fix leaks or tune JVM settings.

16. How to process huge CSV in Spring Boot?

 Use Spring Batch for chunk processing.


 Read data in streams instead of loading fully.
 FlatFileItemReader handles CSV parsing.
 Process and write in batches. Prevents memory overflow issues.
17. How to containerize a Spring Boot app?

 Create a Dockerfile for the app.


 Use openjdk base image. Copy JAR file into container.
 Expose port and define ENTRYPOINT.
 Build and run Docker image.

a
18. How to deploy container in AWS?

al
 Push Docker image to ECR.
 Use ECS or EKS for deployment.
 Define Task Definitions in ECS.
 Configure service and load balancer.
Run containers in AWS cluster.

ew
19. When do you use API Gateway?

 When managing multiple microservices.


 To provide single entry point to clients.
 For security, routing, and monitoring.
 To handle cross-cutting concerns. Useful in microservice architecture.
d

20. How do you block wrong URLs in API Gateway?


Ko

 Define routing rules in Gateway config.


 Use whitelisting/blacklisting of paths.
 Implement custom filters to block.
 Return error responses for invalid routes.
 Protects from misuse and attacks.
21. Where do you configure hostname in microservices on K8s?

 Hostname is set in Service or Ingress.


 Ingress maps domain to service.
 Config defined in YAML manifests.
 DNS points to load balancer.
 K8s resolves routing automatically.

a
22. How do ECS microservices get access to S3?

 Assign IAM role to ECS task.

al
 Role grants S3 bucket permissions.
 ECS container inherits role policies.
 No need to store AWS keys.
 Ensures secure access control.
ew
23. How do you solve performance issues in inter-service calls?

 Use caching to reduce repeated calls.


 Enable async or parallel calls.
 Apply circuit breakers with retries.
 Monitor latency with tracing tools.
 Optimize payloads and serialization.
d
24. SQL: Get total amount category-wise from transaction table.
Ko

 Use GROUP BY clause in SQL. Example: SELECT category, SUM(amount).


 FROM transactions GROUP BY category.
 It aggregates data per category. Provides summarized transaction info.

You might also like