0% found this document useful (0 votes)
16 views

Project Presentation Question and Answers JAVA

Uploaded by

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

Project Presentation Question and Answers JAVA

Uploaded by

harshmavlikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 44

1)What is Framework?

Ans: A framework is a structure that you


can build software on. It serves as a
foundation, so you’re not starting entirely
from scratch. Frameworks are typically
associated with a specific programming
language and are suited to different types
of tasks.

A framework in software development is a


starting point, but you add higher-level
functionality as per your specific need or
requirement.

Some of the advantages of a framework:

 Simpler testing and debugging


 Avoiding duplicate code
 Clean and easily adaptable code
 Able to focus on writing code
specific to the project
 Can be extended

Web application Frameworks

AngularJS is a front-end JavaScript


framework developed and is supported by
Google.

Django is an open-source web


development framework supported by the
Django Software Foundation. It’s written
in Python.
Rails is also an open-source framework.
It’s written in the Ruby programming
language.

Express is a back-end framework for


Node.js that allows you to start and
configure a server with very little
overhead.

Framework vs Library

A library is a set of previously-written code


that you can use to build your own code. A
framework is a supporting structure which
enables you to write specific things as per
the requirement of your project.
Library doesn't enforce you to follow any
pattern, whereas you must follow the
pattern of the framework.

A framework is essentially a skeleton, and


you work with the skeleton. A framework
calls on your code.
Your code calls on a library.
2) What is the difference between
ORM and JDBC?
Ans:-
ORM
Abstracts the database access with
objects.
Objects are mapped to database
tables.
Can automate schema creation and
update. .
Tends to be slower due to the
additional abstraction layer.
JDBC
Directly manages the database access
with SQL queries.
Works with database tables and
columns directly.
Requires manual schema creation and
update
Tends to be faster as it involves direct
SQL queries.

3) What are the main requirements


while designing an Entity in
hibernate?
Ans:- @Entity annotation on the class
and @Id annotation on the field
which you want to treat as a primary
key.
4) How does Maven Dependency
work?
Ans:- In the context of Maven, a
dependency is simply a JAR file used
by a Java application. Based on the
POM file, Maven will download it
from central repository and store it
inside local repository. Java
Application will then be able to find
and use the classes in the JAR file.

5) What is the difference bet'n adding


jar files explicitly in the respective
project vs maven dependency?
Ans:- adding jar files explicitly in the
respective project makes it heavy ,
moreover in future if you want to
add,remove or replace any jar file/s
that gives you maintenance drawback.
Also while transfering project you will
have to transfer all jar files over the
network many times.
On the other hand when you use
maven, There is not any difference at
all. Maven's pom.xml helps to
download all jar files at
.m2/repository location. These jars
can be used for different projects. You
need not to manually download. But
for the very first time you need
internet connection to download all
jar files. In pom.xml we have to set
dependencies like given below or
whatever version of jars you want to
download. If later point in time you
want to change the version ( in
following case ${spring.version} then
you have to change only one place
and everywhere get reflected.

<dependency>

<groupId>org.springframework</grou
pId>

<artifactId>spring-webmvc</artifactId
>

<version>${spring.version}</version>
</dependency>
If you don't use pom.xml then while
transfering project you will have to
transfer all jar files over the network
many times.
Maven give re-usability of jar files in
multiple project. You need not to have
deep copy of all jar files in
WEB-INF/lib folder.

EDIT 1

In very first attempt, any project will


download all jar files from internet
and will store all those jar files
at .m2/repository. Second time in
another project, when you try to use
other pom.xml file then maven will try
to find jars at local (your
computer) .m2/repository whatever
dependencies defined in your new
project's pom.xml if those files are
there in .m2/repository then maven
will not try to connect to internet to
download (because everything is
downloaded at .m2/repository). If
pom.xml file finds new entry then it
will connect to internet and then
download at .m2/repository. In case
you want to add ,remove or replace
any dependency or dependencies you
need to update only .pom file and
changes will be automatically
reflected. Also while transferring the
project , you need to ship only .pom
file to the other side of the network.
This feature makes your project light
weight.

6) Difference between SQL and HQL.


Ans:- SQL is solely based on RDBMSs
but HQL is a combination of OOP with
relational databases. The difference
between SQL and HQL is that SQL is a
standard language used to manage
and manipulate relational databases,
while HQL is a Hibernate Query
Language that is used to query object-
oriented databases.
Conclusion:- SQL deals with tables
and columns whereas HQL deals with
Java classes and instance members.

7)What is Spring?
Ans:- Spring is a framework. Spring
Framework is a Java platform that
provides comprehensive infrastructure
support for developing Java applications.
Spring handles the infrastructure so you
can focus on your application.
Spring enables you to build applications
from “plain old Java objects” (POJOs) and
to apply enterprise services non-invasively
to POJOs. This capability applies to the
Java SE programming model and to full
and partial Java EE.
Examples of how you, as an application
developer, can use the Spring platform
advantage:
 Make a Java method execute in a
database transaction without having to
deal with transaction APIs.
 Make a local Java method a remote
procedure without having to deal with
remote APIs.
 Make a local Java method a
management operation without having
to deal with JMX APIs.
 Make a local Java method a message
handler without having to deal with
JMS APIs.
8) What Are the Benefits of Using
Spring?
Spring targets to make Jakarta EE
development easier, so let’s look at the
advantages:
 Lightweight – There is a slight overhead of
using the framework in development.
 Inversion of Control (IoC) – Spring
container takes care of wiring
dependencies of various objects instead of
creating or looking for dependent objects.
 Aspect-Oriented Programming (AOP) –
Spring supports AOP to separate business
logic from system services.
 IoC container – manages Spring Bean life
cycle and project-specific configurations
 MVC framework – used to create web
applications or RESTful web services,
capable of returning XML/JSON responses
 Transaction management – reduces the
amount of boilerplate code in JDBC
operations, file uploading, etc., either by
using Java annotations or by Spring Bean
XML configuration file
 Exception Handling – Spring provides a
convenient API for translating technology-
specific exceptions into unchecked
exceptions.
9)Name some of the Spring modules.
 Ans:- Core – a key module that provides
fundamental parts of the framework, such as IoC
or DI
 JDBC – enables a JDBC-abstraction layer that
removes the need to do JDBC coding for specific
vendor databases
 ORM integration – provides integration layers for
popular object-relational mapping APIs, such as
JPA, JDO and Hibernate
 Web – a web-oriented integration module that
provides multipart file upload, Servlet listeners and
web-oriented application context functionalities
 MVC framework – a web module implementing
the Model View Controller design pattern
 AOP module – aspect-oriented programming
implementation allowing the definition of clean
method-interceptors and pointcuts

10) What Is Dependency Injection?


Dependency injection is a fundamental
concept in the Spring framework. It refers to a
design pattern where objects are provided
with their dependencies rather than creating
them internally.

In Spring, Dependency Injection (DI) is


achieved through the Inversion of Control
(IoC) container. The container manages the
creation and lifecycle of objects and injects
the required dependencies into the object
when it is created.

The DI mechanism is implemented through


constructor injection, setter injection, or field
injection. Constructor injection involves
passing the required dependencies through
the constructor of the object. Setter injection
involves injecting dependencies through the
object's setter methods, and field injection
involves injecting dependencies directly into
the object's fields.

The advantages of using dependency injection


is loose coupling,flexibility, and testability of
the application. By externalizing the
configuration of dependencies, it becomes
easier to swap out implementations and test
individual components in isolation.

6) How Can We Inject Beans in Spring?


A few different options exist in order to inject
Spring beans:
 Setter injection
 Constructor injection
 Property injection
11) Which injection did you use in your
project?
Ans:- Property Injection

12) What Is a Spring Bean?


The Spring Beans are Java Objects
that are initialized by the Spring IoC
container.

13) Name Some of the


Design Patterns Used in the
Spring Framework?
 Singleton Pattern – singleton-
scoped beans
 Factory Pattern – Bean Factory
classes
 Prototype Pattern – prototype-
scoped beans
 Proxy Pattern – Spring Aspect-
Oriented Programming support
 Template Method
Pattern – JdbcTemplate, Hibernat
eTemplate, etc.
 Front Controller – Spring
MVC DispatcherServlet
 Data Access Object – Spring
DAO support
 Model View Controller – Spring
MVC

14) Explain Spring IOC.


Ans:- Spring IoC (Inversion of Control) Container
is the core of Spring Framework. It creates the
objects, configures and assembles their
dependencies, manages their entire life cycle.
The Container uses Dependency Injection(DI) to
manage the components that make up the
application. It gets the information about the
objects from a configuration file(XML) or Java
Code or Java Annotations and Java POJO class.
These objects are called Beans. Since the
Controlling of Java objects and their lifecycle is
not done by the developers, hence the name
Inversion Of Control. The followings are some of
the main features of Spring IoC,
 Creating Object for us,
 Managing our objects,
 Helping our application to be configurable,
 Managing dependencies

15) What is Service Layer in Spring?


Ans:- A service layer is a layer in an application
that facilitates communication between the
controller and the persistence layer like DAO or
JPARepository.

16) What is Spring Boot?


Ans:- Spring boot is a module of Spring from which
we speed up the development.

It provides an easier and faster way to set up,


configure and run both simple and web-based
applications.
In spring boot we need to provide very less
configuration but it doesn't mean we don't need to
configure anything.

Spring boot = Spring framework + embedded


server - configuration

That means when we use spring boot, we need


less configuration as compare to that when we
don't use spring boot.

17) Explain the working of Spring Boot


Ans:- a) Spring Boot starts by scanning the starter
dependencies in pom.xml , then downloads and
auto-configures the module as you included in
pom.xml.
b) for example if we want to create web
application then we have to put spring-boot-
starter-web dependency in pom.xml. When we
start the project spring boot downloads all the
dependency required for web and configure the
things like spring mvc.
18) explain @SpringBootApplication annotation.
Ans:- This annotation is used to mark the main
class of a Spring Boot application. It
encapsulates @Configuration, @EnableAutoConfigura
tion, and @ComponentScan annotations with their
default attributes.
@Configuration:- @Configuration is a class-level
annotation indicating that an object is a source of bean
definitions .
@Configuration
public class MyConfig
{
@Bean
public Student getStudent()
{
return new Student();
}
}
@EnableConfiguration:- The
@EnableAutoConfiguration annotation enables Spring
Boot to auto-configure the application context.
Therefore, it automatically creates and registers beans
based on both the included jar files in the classpath
and the beans defined by us.
In order to make @Configuration work, you have to
have @EnableAutoConfiguration.
@ComponentScan:- it scans a package/s , looking for
classes that could be automatically registered as beans
in the spring container.

18) How many ways you can run your


Spring boot application?
Ans:- two ways
a) right click on Spring Boot
Application java file and
run as
run as Java Application
or
b) create jar file of your entire Spring
boot application and run with the
command
java -jar <the jar file name>
19) How does spring boot application start?
Ans:- a) starts by calling main() function of
SpringBootApplication class (the one which has
been annotated with @SpringBootApplication).
The @SpringBootApplication annotation is a
combination of the following three Spring
annotations: @EnablAutoConfiguration,@Configur
ation, and @ComponentScan.
b) the run() method of SpringApplication class is
called. This method starts the application by
creating application context and initializing it.
c) once the application context is initialized, the
run() method starts the application’s embedded
server.
20) What are the top Spring Boot annotations?
Ans:-
@SpringBootApplication: it combines three
annotations @Configuration,
@EnableAutoConfiguration and
@ComponentScan
@Component: to make a particular class as
bean.
@Autowired:- to inject a particular reference
with the required bean.
@Service:- This annotation is used to indicate
that a class represents a service component in
the application. It is typically used to annotate
classes that contain logic to communicate with
the repository layer.
@RestController:- is a combination of
@Controller and @ResponseBody
@RequestMapping:- used to map specific url
to a method. Used on class as well as method
level. It can be used with method attribute for
GET/POST/PUT/DELETE operations.
@Repository:- marks the class as DAO. It is
used on the class that has database persistent
logic.
21) In normal Spring MVC application we
configure application using Spring Bean
Configuration file (xml file). In Spring Boot , how
do we do the same?
Ans:- In Spring boot application we need not
create any Spring Bean Configuration file ( xml
file ), rather we write configuration details inside
“application.properties” file.
22) Is it possible to change the port no. of
embedded Tomcat Server in Spring Boot?
Ans:- Yes it’s possible. By using the server.port in
application.properties file.

23) Can we override or replace the Embedded


Tomcat Server in Spring boot?
Ans:- Yes, we can replace the Embedded Tomcat
server with any server by using the Starter
dependency inside pom.xml file.

<dependency>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</groupId>
<exclusions>
<exclusion>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</groupId>
</exclusion>
</exclusions>
</dependency>

and now you need to include new server as


dependency i.e.

<dependency>
<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jetty</groupId>
</dependency>

23) What is auto-configuration in spring


boot? How does it help?
Ans:- Spring boot automatically configures a lot of
things based upon what is present in the classpath.
e.g. while creating spring starter project if you
choose “spring web” as a dependency,
automatically following entries will be added
inside the generated “pom.xml” file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
24) Why Spring Boot is called as
opinionated?
Ans:-
Spring boot automatically configures a lot of
dependencies just by its availability in the
classpath. For example, it can auto configure
tomcat if the server container is not available. This
is why spring boot is opinionated because it auto-
configure many dependencies if it is not needed
and we can override auto-configuration settings as
needed.
Auto configuration is disabled by default and use
one of the
annotations @SpringBootApplication or @Enable
AutoConfiguration on the Main class to enable the
feature.
25) What is the difference between
RestController and Controller?
Ans:- @Controller is supposed to return a
response to the client through View ( either JSP or
Thymeleaf). Whereas @RestController
( @Controller + @ResponseBody ) is supposed to
return the response directly to the client in the
form of JSON or text or html or xml. There is no
View in case of RestController.
26) What embedded containers does Spring
Boot support?
Ans:- Spring Boot includes support for embedded
Tomcat, Jetty, and Undertow servers.
By default the embedded server will listen for
HTTP requests on port 8080.
27) What is the difference between
embedded container and a war?
Ans:- The main difference between an embedded
container and a war is that you can run Spring boot
application as a jar from commad prompt without
setting up a web server.
But to run a war file , you need to first set up a
web container like Tomcat and then deploy war file
there.
28) Explain JPA Repository in Spring.
Ans:- JPA is an abbreviation for JAVA
Persistence API (Application program
interface). As the name suggests, JPA helps in
persisting the java objects in relational
databases.
In the Java Spring framework, JPA-based
repositories are commonly used. These
repositories are crucial in managing database
operations and are a part of the Spring Data
JPA module. Repositories define a new
elegant method of storing, updating, and
extracting the data stored from JAVA
applications in the backend. All of the CRUD
(Create, read, update, and delete) operations
can be implemented with the help of a
repository interface.

29) What are the two ways to


configure required things e.g. server
port, secret key of JWT, database
details in the spring boot
application?
Ans:- application.properties file and
application.yml file.
30) What is Spring boot devtools?
Ans:- In a typical application development
environment, a developer would make some changes,
build the project and deploy/start the application again
for new changes to take effect.
Using spring-boot-devtools, this process is also
automated. Whenever files change in the classpath,
applications using spring-boot-devtools will cause the
application to restart automatically.

31) How do you achieve transaction in spring


boot application?
Ans:- e.g.
public class UserService {

@Transactional
public Long registerUser(User user) {
// execute some SQL that e.g.
// inserts the user into the db and retrieves
the autogenerated id
// userDao.save(user);
return id;
}
}

 Make sure that your Spring Configuration is


annotated with the
@EnableTransactionManagement annotation (In
Spring Boot this will be done automatically for
you).
 Make sure you specify a transaction manager in
your Spring Configuration (this you need to do
anyway).
 And then Spring is smart enough to transparently
handle transactions for you: Any
bean’s public method you annotate with the
@Transactional annotation, will execute inside a
database transaction
when we say Spring transparently handles transactions
for you. What does that really mean?
Armed with the knowledge from the JDBC transaction
example, the @Transactional UserService code above
translates (simplified) directly to this:
public class UserService {

public Long registerUser(User user) {


Connection connection =
dataSource.getConnection(); // (1)
try (connection) {
connection.setAutoCommit(false); // (1)

// execute some SQL that e.g.


// inserts the user into the db and retrieves the
autogenerated id
// userDao.save(user); <(2)

connection.commit(); // (1)
} catch (SQLException e) {
connection.rollback(); // (1)
}
}
}
This is what Spring’s transactional annotation does
for you automatically, without you having to write
it explicitly.

32) What is the difference between @Inject


and @Autowired?
Ans:- @Autowired

@Autowired
Private CustomerService service;

Here IOC container instantiates an


implementation of CustomerService interface
and injects it to “service” reference.
The @Inject annotation also serves the same
purpose as @Autowired. The main difference
between them is that @Inject is a standard
annotation for dependency injection and
@Autowired is a spring specific (can be used
only in the spring framework) .
If you want the dependency injection you use
should not be limited to only spring framework
but also should be used in some other
frameworks , you should use @Inject because
it’s a standard which can be used in some other
frameworks e.g. google’s dependency manager.
So if you want to your project should work in
other than spring framework also, use @Inject
whereas if you want to work only in spring
framework, use @Autowired.
e.g.
JVM is just a standard or a specification and its
implementations are different JDKs like Oracle
JDK, OpenJDK, IBM jdk, Amazon JDK etc.
Similarly @Inject is a standard or specification
and @Autowired is an implementation of
@Inject specification or standard.

33) What is the difference between @Bean


and @Component?
Ans:- @Component annotation is preferable
for component scanning and automatic wiring.
The class on which you apply @Component
annoation, will be instantiated automatically
and also will be injected to the required
reference ( where we’ve specified @Autowired)
On the other hand, if you want to create a bean
using a specific logic i.e. you would like to
create the bean the way you want, you will use
@Bean annotation.
@Bean annotation returns an object that
spring should register as bean in application
context. The body of the method contains the
logic responsible for creating the instance.

e.g.

@Component

@Component
public class Student
{
default constructor
other stuff
}

@Component
public class Course
{
@Autowired
private Student student;
}

instance of Student class will be created


automatically (which will be considered as a
bean) and then will be injected to "student"
reference inside "Course" class.

@Bean
public class Student
{
parameterized constructor accepting rollno
other stuff
}

@Configuration
public class MyConfiguration
{
@Bean
public Student getStudent()
{
// some logic to create Student instance
Student s1=new Student(random
number);
return s1;
}
}

@Component
public class Course
{
@Autowired
private Student student;
}

in the above code the method annotated with


@Bean will be invoked automatically , create
the instance of Student (which will be
considered as a bean) and then will be injected
to "student" reference inside "Course" class.

34) What is exactly JWT?


Ans:-
JWT is a way by which two parties can
communicate in a secured way. It's a
standard way provided by "RFC
7519".
JWT is a JSON Web token which is
created on the server ,given to the
client who uses it for subsequent
requests. It is also used to have
communication between two
microservices in a secured way.
JWT mainly supports authorization
and not the authentication.

35) What is the difference between


Front Controller and Controller in
Spring?
Ans:- The Front Controller stands
first that is why it's name is like this.
After the requests comes into this,
the dispatcher servlet
(DispatcherServlet in-built servlet
provided by Spring Framework)
accepts the requests and decides
which controller will be suitable to
handle these requests. Then it
dispatches the HTTP requests to
specific controller (which
programmer defines).
36) Where have you used IOC in your
project?
Ans:-
We used @Autowired for Service
layer inside the RestControllers and
for Repository layer inside the service
implementations.
37) What is DAO?
Ans:- DAO decouples business layer
from a particular persistence
mechanism [ file,database etc]. So
business layer can concentrate only
on business logic.
38) What is monolothic architecture?
Ans:- A monolithic application is built
as a single unit. Enterprise
applications are built in three parts:
• A database — consisting of many
tables usually in a relational
database management system
• A client-side user interface —
consisting of HTML pages and/or
JavaScript running in a browser)
• A server-side application — which
will handle HTTP requests,
execute domain-specific logic,
retrieve and update data from the
database, and populate the HTML
views to be sent to the browser.
This is what makes an architecture
monolith — it is a single logical
executable. To make any
changes to the system, a
developer must build and deploy
an updated version of the
server-side application.

39) What are MicroServices?


Ans:- Microservices are the small
services that work together. E.g.
CartService (one project) and
ProductService (another project)
These small services communicate
with each other directly using light
weight protocol like HTTP.

40) What is CORS?


Ans:- Cross-Origin Resource Sharing
(CORS) is an HTTP-header based
mechanism that allows a server to
indicate any origins (domain, scheme,
or port) other than its own from
which a browser should permit
loading resources.

What is origin?

origin consists of
"protocol" ,"domain" and "port"

e.g. http://myserver.com:8000
here "http" - protocol
"myserver.com" - domain name
8000 - port

By default browsers prevent Ajax


request from different origin.

CORS is a standard to relax the same


origin policy.

It selectively allows cross origin


requests while rejecting others.

how can we enable CORS?


apply following annotation on the
RestController class

@CrossOrigin("http://localhost:4200"
)
here "http://localhost:4200" is
assumed to be a different origin from
where you would like to allow access
to your Rest Service/s.

You might also like