Interview Questions
Interview Questions
S.No Topic
1 CoreJava
2. JDBC
3. Hibernate
4. Spring
5. Spring Boot interview
Core Java Interview Questions
1. Which is superclass of all your java object
Ans: object class
Constructor does not have return type but method must have return type
Constructor name should be the same name as class name but method may or
maynot
32. How many ways we can print exception message in java program..?
Ans: We can print the exception message in different ways
1. by using printstackTrace()----->Trace the exception line of code
2. getMessage()
3. toString()
4. print the exception class ref
33)What all are different concepts of OOPS(Object Oriented Programming
System)
Encapsulation
Abstraction
Polymorphism
Inheritance
34)What is the difference between abstract classes and interfaces?
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since
abstract methods. Java 8, it can have default and static
methods also.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
6) An abstract class can extend another Java An interface can extend another Java interface
class and implement multiple Java interfaces. only.
7) An abstract class can be extended using An interface can be implemented using keyword
keyword "extends". "implements".
8) A Java abstract class can have class Members of a Java interface are public by default.
members like private, protected, etc.
In JDBC, Statements are used to send SQL commands to the database and receive data
from the database. There are various methods provided by JDBC statements such as
execute(), executeUpdate(), executeQuery, etc. which helps you to interact with the
database.
Statements Explanation
Statement Statement is the factory for resultset. It is used for general purpose access
to the database. It executes a static SQL query at runtime.
The execute method can be used The executeQuery The executeUpdate method can
for any SQL statements(Select and method can be used only be used to update/delete/insert
Update both). with the select operations in the database.
statement.
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned
before the first row. The next method moves the cursor to the next row, and because it returns false when
there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result
set.
39)
Hibernate Interview Questions
1. It removes a lot of repetitive code from the JDBC API, and therefore, the
code is easier to read, write, and support.
2. Hibernate Query Language (HQL) is fully object-oriented and more close to
the Java programming language comparing to SQL in JDBC. Also, HQL is
a Database-independent query that allows you to switch between different
databases easily.
3. Hibernate supports caching, which improves performance.
- First-level is a mandatory Session cache.
- Second-level is an optional cache. Hibernate has a lot of cache providers
for this level, the most popular are: EHCache, OSCache, warmCache, JBoss
Cache, etc.
- Query-level is an optional cache for query result sets.
4. Lazy load that also improves performance.
5. Hibernate supports JPA annotations, which means the code is portable to
other ORM frameworks.
6. Hibernate has a connection pool.
7. You do not need to handle exceptions.Hibernate allows database
management (for example creating tables), JDBC can only work with
existing DB tables.
8. Hibernate supports inheritance, associations, and collections that are not
available in the JDBC API.
Spring Interview Questions
List the advantages of Spring Framework.
• Because of Spring Frameworks layered architecture, you can use what you
need and leave which you don’t.
• Spring Framework enables POJO (Plain Old Java Object) Programming which
in turn enables continuous integration and testability.
• JDBC is simplified due to Dependency Injection and Inversion of Control.
• It is open-source and has no vendor lock-in.
5. How many modules are there in Spring Framework and what are
they?
There are around 20 modules which are generalized into Core Container, Data
Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation and
Test.
a. Spring Core
b. Spring Bean
c. SpEL (Spring Expression Language)
d. Spring Context
• Web – This layer provides support to create web application. It contains the
following modules :
a. Web
b. Web – MVC
c. Web – Socket
d. Web – Portlet
A Spring configuration file is an XML file. This file mainly contains the classes
information. It describes how those classes are configured as well as introduced to
each other. The XML configuration files, however, are verbose and more clean. If it’s
not planned and written correctly, it becomes very difficult to manage in big projects.
Spring Framework can be used in various ways. They are listed as follows:
At the core of the Spring Framework, lies the Spring container. The container creates
the object, wires them together, configures them and manages their complete life
cycle. The Spring container makes use of Dependency Injection to manage the
components that make up an application. The container receives instructions for
which objects to instantiate, configure, and assemble by reading the configuration
metadata provided. This metadata can be provided either by XML, Java annotations
or Java code.
In Dependency Injection, you do not have to create your objects but have to describe
how they should be created. You don’t connect your components and services
together in the code directly, but describe which services are needed by which
components in the configuration file. The IoC container will wire them up together.
• Constructor Injection
• Setter Injection
• Interface Injection
It works better for many properties. It works better for few properties.
BeanFactory vs ApplicationContext
BeanFactory ApplicationContext
It explicitly provides a resource object using the syntax It creates and manages resource objects on its own
Let’s move on to the next section of Spring Interview Questions, that is Spring Beans
Interview Questions.
• They are the objects that form the backbone of the user’s application.
• Beans are managed by the Spring IoC container.
• They are instantiated, configured, wired and managed by a Spring IoC container
• Beans are created with the configuration metadata that the users supply to the
container.
3 </bean>
1 <beans>
2 <context:annotation-config/>
4 </beans>
• Singleton: This provides scope for the bean definition to single instance per
Spring IoC container.
• Prototype: This provides scope for a single bean definition to have any number
of object instances.
• Request: This provides scope for a bean definition to an HTTP-request.
• Session: This provides scope for a bean definition to an HTTP-session.
• Global-session: This provides scope for a bean definition to an Global HTTP-
session.
19. What is the Bean life cycle in Spring Bean Factory Container?
1. The Spring container instantiates the bean from the bean’s definition in the XML
file.
2. Spring populates all of the properties using the dependency injection, as
specified in the bean definition.
3. The factory calls setBeanName() by passing the bean’s ID, if the bean
implements the BeanNameAware interface.
4. The factory calls setBeanFactory() by passing an instance of itself, if the bean
implements the BeanFactoryAware interface.
5. preProcessBeforeInitialization() methods are called if there are any
BeanPostProcessors associated with the bean.
6. If an init-method is specified for the bean, then it will be called.
7. Finally, postProcessAfterInitialization() methods will be called if there are any
BeanPostProcessors associated with the bean.
a. no: This is default setting which means no autowiring. Explicit bean reference
should be used for wiring.
b. byName: It injects the object dependency according to name of the bean. It
matches and wires its properties with the beans defined by the same names in
the XML file.
c. byType: It injects the object dependency according to type. It matches and
wires a property if its type matches with exactly one of the beans name in
XML file.
d. constructor: It injects the dependency by calling the constructor of the class.
It has a large number of parameters.
e. autodetect: First the container tries to wire using autowire by constructor, if it
can’t then it tries to autowire by byType
Following are some of the limitations you might face with auto wiring:
Instead of using XML to describe a bean wiring, the developer moves the configuration
into the component class itself by using annotations on the relevant class, method, or
field declaration. It acts as an alternative to XML setups. For example:
1 @Configuration
2 public class AnnotationConfig
3 {
4 @Bean
5 public MyDemo myDemo()
{ return new MyDemoImpll(); }
6 }
7
25. How annotation wiring can be turned on in Spring?
By default, Annotation wiring is not turned on in the Spring container. Thus, to use
annotation based wiring we must enable it in our Spring configuration file by
configuring <context:annotation-config/> element. For example:
@Component: This marks a java class as a bean. It is a generic stereotype for any
Spring-managed component. The component-scanning mechanism of spring now can
pick it up and pull it into the application context.
@Controller: This marks a class as a Spring Web MVC controller. Beans marked with
it are automatically imported into the Dependency Injection container.
For example:
1
public class Employee
2 {
3 private String name;
4 @Required
5 public void setName(String name)
6 {this.name=name; }
public string getName()
7 { return name; }
8 }
9
28. What do you understand by @Autowired annotation?
The @Autowired annotation provides more accurate control over where and how
autowiring should be done. This annotation is used to autowire bean on the setter
methods, constructor, a property or methods with arbitrary names or multiple
arguments. By default, it is a type driven injection.
For Example:
1
public class Employee
2 {
3 private String name;
4 @Autowired
5 public void setName(String name)
6 {this.name=name; }
public string getName()
7 { return name; }
8 }
9
29. What do you understand by @Qualifier annotation?
When you create more than one bean of the same type and want to wire only one of
them with a property you can use the @Qualifier annotation along
with @Autowired to remove the ambiguity by specifying which exact bean should be
wired.
For example, here we have two classes, Employee and EmpAccount respectively. In
EmpAccount, using @Qualifier its specified that bean with id emp1 must be wired.
Employee.java
1
2 public class EmpAccount
{
3 private Employee emp;
4 @Autowired
5 @Qualifier(emp1)
6 public void showName()
{
7 System.out.println(“Employee name : ”+emp.getName);
8 }
9 }
10
30. What do you understand by @RequestMapping annotation?
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.
Q4. What are the @RequestMapping and @RestController
annotation in Spring Boot used for?
@RequestMapping @RestController
This annotation is used to provide the
This annotation is used to add the
routing information and tells to Spring
@ResponseBody and @Controller
that any HTTP request must be mapped
annotation to the class
to the respective method.
To use this annotation, you have to To use this annotation, you have to
import org.springframework.web. import org.springframework.web.
bind.annotation.RequestMapping; bind.annotation.RestController;
consumes = “text/plain”
consumes = {“text/plain”, “application/*”}