Q&A JAVA (1)
Q&A JAVA (1)
2. What are the main features of Java? Answer: Key features include
portability, object-oriented, robust, secure, architecture-neutral, and high
performance.
5. What is the purpose of the main method in Java? Answer: The main
method is the entry point of a Java application. It is where the program starts
its execution.
o Characteristics:
o Characteristics:
o Characteristics:
Pause Times: Aims for low pause times but may still
experience some pauses during the remark phase.
o Characteristics:
Region-based: Divides the heap into fixed-size regions and
manages garbage collection within these regions.
o Characteristics:
o Characteristics:
Each garbage collector has its own strengths and is suited for different types of
applications and environments. Choosing the right garbage collector depends on
the specific needs of the application, such as latency requirements, throughput
needs, and heap size.
5. How do you analyze memory leaks in Java? Answer: Use profiling tools
like VisualVM, Eclipse Memory Analyzer (MAT), or Java Mission Control to detect
and analyze memory leaks.
Java Collections
1. What is the difference between ArrayList and LinkedList? Answer:
1. ArrayList: Backed by a dynamic array, providing fast random access
but slower insertions and deletions.
4. What is a Set in Java? Answer: A Set is a collection that does not allow
duplicate elements. Common implementations include HashSet and TreeSet.
Multithreading in Java
1. What is the difference between synchronized and volatile? Answer:
2. What is the Thread class in Java? Answer: The Thread class represents a
thread of execution in a program. It provides methods to start, run, and
manage threads.
3. How do you create a thread in Java? Answer: You can create a thread by
extending the Thread class or implementing the Runnable interface and passing
it to a Thread object.
2. What are the main features of Spring? Answer: Key features include
Dependency Injection (DI), Aspect-Oriented Programming (AOP), Transaction
Management, MVC Framework, and Data Access Integration.
7. What is the Spring IoC Container? Answer: The Spring IoC Container is
responsible for managing the lifecycle and configuration of application objects
(beans). It is configured using XML, annotations, or Java configuration.
3. Request: A new instance is created for each HTTP request (only in web
applications).
4. Session: A new instance is created for each HTTP session (only in web
applications).
14. What is the difference between @Before, @After, and @Around advice?
Answer:
3. @Around: Wraps the join point and can control when the join point is
executed (before, after, or not at all).
15. What is Spring MVC? Answer: Spring MVC is a web framework that provides
a model-view-controller architecture for building web applications. It separates
the application into model, view, and controller layers.
17. How does Spring Boot differ from the Spring Framework? Answer:
Spring Boot is a project built on top of the Spring Framework that simplifies the
setup and development of Spring applications by providing production-ready
features, auto-configuration, and a convention-over-configuration approach.
24. How do you create a RESTful API with Spring Boot? Answer: Create a
RESTful API by using @RestController to define endpoints, @RequestMapping to
map requests to methods, and return responses in JSON format.
25. What is Actuator in Spring Boot? Answer: Spring Boot Actuator provides
production-ready features such as monitoring, metrics, and health checks. It
exposes endpoints to manage and monitor the application.
27. What is Spring Boot DevTools? Answer: It is a set of tools to improve the
development experience, including features like automatic restarts, live reload,
and configuration properties management.
28. How do you create a scheduled task in Spring Boot? Answer: Use the
@Scheduled annotation on a method to define scheduled tasks, and enable
scheduling with @EnableScheduling.
30. What are Spring Boot’s embedded servers? Answer: Spring Boot
provides embedded servers like Tomcat, Jetty, and Undertow, allowing
applications to run as standalone Java applications without needing an external
server.
31. How do you configure multiple data sources in Spring Boot? Answer:
Define multiple DataSource beans and configure them using @Primary for the
default DataSource and @Qualifier for others.
32. What is @Value annotation used for? Answer: It is used to inject values into
fields from configuration files or environment variables.
34. What is @SpringBootTest used for? Answer: It is used for integration testing
of Spring Boot applications, providing a complete Spring application context for
testing.
35. How do you create a Spring Boot application with embedded Jetty?
Answer: Include spring-boot-starter-jetty dependency and exclude Tomcat
from spring-boot-starter-web.
36. What is @EnableAutoConfiguration used for? Answer: It enables Spring
Boot’s auto-configuration feature to automatically configure application
components based on the dependencies present.
37. How do you secure a Spring Boot application? Answer: Use Spring
Security to configure authentication, authorization, and protection mechanisms
in your application.
39. How do you configure logging in Spring Boot? Answer: Configure logging
through application.properties or application.yml, and use logging
frameworks like Logback, Log4j2, or Java Util Logging.
42. How do you define a REST API with Spring Boot? Answer: Use
@RestController to define a controller, and map HTTP requests to methods
using @RequestMapping, @GetMapping, @PostMapping, etc.
43. What is @Profile annotation used for? Answer: It is used to define different
beans and configurations for different environments or profiles (e.g.,
development, production).
44. How do you test a Spring Boot application? Answer: Use testing
frameworks like JUnit and Spring Test for unit and integration testing. Utilize
@SpringBootTest for context loading and end-to-end tests.
47. What is the role of Spring Boot Initializr? Answer: It is an online tool that
generates a Spring Boot project structure with dependencies, configurations,
and boilerplate code based on user inputs.
49. What is the @Bean annotation used for? Answer: It is used to define a bean
in a @Configuration class and manage its lifecycle in the Spring context.
54. What is @EntityScan used for? Answer: It is used to specify the packages to
scan for JPA entities, especially when entities are in different packages from the
main application.
55. How do you handle HTTP sessions in Spring Boot? Answer: Use
HttpSession to manage user sessions and @SessionAttributes to store session
attributes.
56. What is the Spring Boot CLI? Answer: It is a command-line tool for
developing and testing Spring Boot applications using Groovy or Java.
57. What is @Component annotation used for? Answer: It is used to mark a class
as a Spring-managed component, making it eligible for component scanning
and dependency injection.
58. How do you create a custom error page in Spring Boot? Answer: Create
an HTML page named error.html or configure error pages through
application.properties using server.error.whitelabel.enabled=false.
59. What is @RequestBody annotation used for? Answer: It is used to bind the
HTTP request body to a method parameter in a controller, usually for handling
JSON or XML data.
61. How do you enable and configure caching in Spring Boot? Answer: Use
@EnableCaching annotation to enable caching and configure caching behavior
using CacheManager and Cache implementations.
62. How do you define a REST API with Spring Boot? Answer: Use
@RestController to define a controller, and map HTTP requests to methods
using @RequestMapping, @GetMapping, @PostMapping, etc.
63. What is @Profile annotation used for? Answer: It is used to define different
beans and configurations for different environments or profiles (e.g.,
development, production).
64. How do you test a Spring Boot application? Answer: Use testing
frameworks like JUnit and Spring Test for unit and integration testing. Utilize
@SpringBootTest for context loading and end-to-end tests.
67. What is the role of Spring Boot Initializr? Answer: It is an online tool that
generates a Spring Boot project structure with dependencies, configurations,
and boilerplate code based on user inputs.
68. How do you handle CORS (Cross-Origin Resource Sharing) in Spring
Boot? Answer: Configure CORS by using @CrossOrigin annotation on
controllers or global CORS configuration through WebMvcConfigurer.
69. What is the @Bean annotation used for? Answer: It is used to define a bean
in a @Configuration class and manage its lifecycle in the Spring context.
74. What is @EntityScan used for? Answer: It is used to specify the packages to
scan for JPA entities, especially when entities are in different packages from the
main application.
75. How do you handle HTTP sessions in Spring Boot? Answer: Use
HttpSession to manage user sessions and @SessionAttributes to store session
attributes.
76. What is the Spring Boot CLI? Answer: It is a command-line tool for
developing and testing Spring Boot applications using Groovy or Java.
77. What is @Component annotation used for? Answer: It is used to mark a class
as a Spring-managed component, making it eligible for component scanning
and dependency injection.
78. How do you create a custom error page in Spring Boot? Answer: Create
an HTML page named error.html or configure error pages through
application.properties using server.error.whitelabel.enabled=false.
79. What is @RequestBody annotation used for? Answer: It is used to bind the
HTTP request body to a method parameter in a controller, usually for handling
JSON or XML data.
81. How do you enable and configure caching in Spring Boot? Answer: Use
@EnableCaching annotation to enable caching and configure caching behavior
using CacheManager and Cache implementations.