Qustions For Interview Preparation
Qustions For Interview Preparation
Spring Boot is a Spring module that aims to simplify the use of the Spring
framework for Java development. It is used to create stand-alone Spring-
based applications that you can just run. So, it basically removes a lot of
configurations and dependencies. Aiming at the Rapid Application
Development, Spring Boot framework comes with the auto-dependency
resolution, embedded HTTP servers, auto-configuration, management
endpoints, and Spring Boot CLI.
So, if you ask me why should anybody use Spring Boot, then I would say,
Spring Boot not only improves productivity but also provides a lot of
conveniences to write your own business logic.
There is no doubt in the fact that Spring Boot allows the developers to run
the same application in different environments. Well, this is done with the
support it provides for external configuration. It uses environment
variables, properties files, command-line arguments, YAML files, and
system properties to mention the required configuration properties. Also,
the @value annotation is used to gain access to the properties. So, the
most possible sources of external configuration are as follows:
Q8. What are the Spring Boot starters and what are
available the starters?
Spring Actuator is a cool feature of Spring Boot with the help of which you
can see what is happening inside a running application. So, whenever you
want to debug your application, and need to analyze the logs you need to
understand what is happening in the application right? In such a scenario,
the Spring Actuator provides easy access to features such as identifying
beans, CPU usage, etc. The Spring Actuator provides a very easy way to
access the production-ready REST points and fetch all kinds of information
from the web. These points are secured using Spring Security’s content
negotiation strategy.
Q10. What is Spring Boot dependency management?
1<dependency>
2<groupId>org.springframework.boot</groupId>
3<artifactId>spring-boot-starter-thymeleaf</artifactId>
4</dependency>
Q13. Can we change the port of the embedded Tomcat
server in Spring boot?
Yes, we can change the port of the embedded tomcat server by using the
application properties file. In this file, you have to add a property of
“server.port” and assign it to any port you wish to. For example, if you
want to assign it to 8081, then you have to mention server.port=8081.
Once you mention the port number, the application properties file will be
automatically loaded by Spring Boot and the required configurations will
be applied on to the application.
Spring Boot Dev Tools are an elaborated set of tools and aims to make the
process of developing an application easier. If the application runs in the
production, then this module is automatically disabled, repackaging of
archives are also excluded by default. So, the Spring Boot Developer Tools
applies properties to the respective development environments. To
include the DevTools, you just have to add the following dependency into
the pom.xml file:
1<dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-devtools</artifactId>
4</dependency>
Q15. Mention the steps to create a Spring Boot project
using Spring Initializer.
Spring Initializr is a web tool provided by Spring. With the help of this tool,
you can create Spring Boot projects by just providing project details. The
following steps need to be followed to create a Spring Boot project using
Spring Initializer:
Choose the maven project and the required dependencies. Then, fill
in the other required details like Group, Artifact, and then click on
Generate Project.
Once the project is downloaded, extract the project onto your
system
Next, you have to import this project using the import option on the
Spring Tool Suite IDE
While importing the project, remember that you have to
choose the project type to be Maven and the source project
should contain the pom.xml file.
Once, all the above steps are followed you will see that the Spring Boot
project is created with all the required dependencies.
Spring Boot starter projects provide the required libraries to connect the
application with JDBC. So, for example, if you just have to create an
application and connect it with MySQL database, you can follow the below
steps:
1CREATE TABLE customers(customerid INT PRIMARY KEY NOT NULL AUTO_INCREMENT, customernam
Step 3: Now, create a Spring Boot project and provide the required
details
Step 5: Once the project is created, you have to configure the database
into application properties
1spring.datasource.url=jdbc:mysql://localhost:3306/example
2spring.datasource.username=root
3spring.datasource.password=edureka
4spring.jpa.hibernate.ddl-auto=create-drop
Step 6: The main application.java class should have the following code:
1package com.edureka;
2import org.springframework.boot.SpringApplication;
3import org.springframework.boot.autoconfigure.SpringBootApplication;
4@SpringBootApplication
5public class SampleApplication {
6 public static void main(String[] args) {
7 SpringApplication.run(SampleApplication.class, args);
8 }
9}
The advantages of the YAML file than a properties file is that the data is stored in a
hierarchical format. So, it becomes very easy for the developers to debug if there is an issue.
The SpringApplication class supports the YAML file as an alternative to properties whenever
you use the SnakeYAML library on your classpath. The different ways to load a YAML file
in Spring Boot is as follows:
When we use the Spring Boot Auto Configuration, automatically the spring-boot-starter-
data-jpa dependency gets added to the pom.xml file. Now, since this dependency has a
transitive dependency on JPA and Hibernate, Spring Boot automatically auto-configures
Hibernate as the default implementation for JPA, whenever it sees Hibernate in the classpath.
Spring Data REST is used to expose the RESTful resources around Spring Data repositories.
Consider the following example:
1{
2"customername": "Rohit"
3}
Response Content
1{
"customername": "Rohit"
2"_links": {
3"self": {
4"href": "<a href="http://localhost:8080/sample/1">http://localhost:8080
5a>"
6},
7"sample": {
8"href": "<a href="http://localhost:8080/sample/1">http://localhost:8080
a>"
9
}
1
0}
Observe that the response content contains the href of the newly created resource.
consumes = “text/plain”
consumes = {“text/plain”, “application/*”}
The boundary of the transaction should start from the Service Layer since the logic for the
business transaction is present in this layer itself.
path – This section is used to mention the segment under which the resource is to be
exported.
collectionResourceRel – This value is used to generate links to the collection resource.
Since Spring Boot supports Log4j2 for logging a configuration, you have to exclude Logback
and include Log4j2 for logging. This can be only done if you are using the starters project.
For example, If you want to work with Spring MVC, you can include
“spring–boot–starter–web” as a dependency in pom.xml.
What is @pathVariable?
@PathVariable annotation helps you to extract information from the
URI directly.
Below are some key points which spring boot offers but spring doesn’t:
Starter POM.
Version Management.
Auto Configuration.
Component Scanning.
Embedded server.
InMemory DB.
Actuators
Spring boot provides numbers of starter dependency, here are the most
commonly used -
Spring Boot application scans all the beans and package declarations
when the application initializes. You need to add the @ComponentScan
annotation for your class file to scan your components added to your
project.
Just like any other Java program, a Spring Boot application must have a
main method. This method serves as an entry point, which invokes the
SpringApplication#run method to bootstrap the application.
@SpringBootApplication
public class MyApplication {
SpringApplication.run(MyApplication.class);
// other statements
}
}
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
11. What is Spring Boot CLI and what are its benefits?
//use of exclude
@EnableAutoConfiguration(exclude={className})
RequestMapping can be used with GET, POST, PUT, and many other
request methods using the method attribute on the annotation. Whereas
getMapping is only an extension of RequestMapping which helps you to
improve on clarity on request.
To make this easy and clean, Spring has the provision of Profiles to keep
the separate configuration of environments.
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-actuator </artifactId>
</dependency>
Health
Info
Beans
Mappings
Configprops
Httptrace
Heapdump
Threaddump
Shutdown
29. How to get the list of all the beans in your Spring boot
application?
Spring Boot actuator “/Beans” is used to get the list of all the spring
beans in your application.
Spring Boot actuator “/env” returns the list of all the environment
properties of running the spring boot application.
You can define both application and Spring boot-related properties into a
file called application.properties. You can create this file manually or use
Spring Initializer to create this file. You don’t need to do any special
configuration to instruct Spring Boot to load this file, If it exists in
classpath then spring boot automatically loads it and configure itself and
the application code accordingly.
The process of injecting dependent bean objects into target bean objects
is called dependency injection.
Setter Injection: The IOC container will inject the dependent bean object
into the target bean object by calling the setter method.
Constructor Injection: The IOC container will inject the dependent bean
object into the target bean object by calling the target bean constructor.
Field Injection: The IOC container will inject the dependent bean object
into the target bean object by Reflection API.
Spring Boot application scans all the beans and package declarations
when the application initializes. You need to add the @ComponentScan
annotation for your class file to scan your components added to your
project
Spring boot provides numbers of starter dependency, here are the most
commonly used -
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
JAVA Questions
1. What is Java?
2. Define Object in Java?
3. What is typecasting?
4. How many types of operators are available in Java?
5. What are the bitwise operators in Java?
6. List out the control statements in Java?
7. Describe in brief OOPs concepts?
8. What is a static variable?
9. What is the usage of this keyword in Java?
10. Can we have virtual functions in Java?
11. How many types of operators are available in Java?
12. What is the significant difference between JVM, JRE,
and JDK?
13. What are primitive data types?
14. What is Typecasting?
15. What are Java keywords?
Reference
https://www.simplilearn.com/tutorials/java-tutorial/java-interview-
questions
https://www.digitalocean.com/community/tutorials/java-string-
interview-questions-and-answers
https://www.codingninjas.com/codestudio/library/30-java-string-
interview-questions
https://www.edureka.co/blog/interview-questions/java-collections-
interview-questions/