java

Java Viva Questions (Part 1)

Here is a list of some Java Viva Questions and their answers also. There are more than twenty questions which you can read.

Below are the Java Viva Questions

Ques  –  What are Annotations in java?

Ans – Annotations are used to provide supplemental information about a program. Annotations start with ‘@’. Annotations do not change the action of a compiled program. Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.

 

Ques – Life cycle of servlets.

Ans – Life cycle of servlets :-

  1. Servlet class is loaded.
  2. Servlet instance is created.
  3. init method is invoked.
  4. service method is invoked.
  5. destroy method is invoked.

 

Ques – Life cycle of entity 

Ans – Entity instances can be in any one of these four states: new, managed, detached, or removed.

  1. The state transitions of an Entity depend upon the type of operations performed on the Entity.
  2.  New/Transient state represents an entity object which is created newly and is not associated with the persistence context even once
  3.  Managed state represents an entity object on which one of the following method is invoked: find, persist. When entities move to managed state they are associated with the persistence context
  4.  Detached state represents an entity object that was once managed in the persistence context and after closing/clearing the context is no longer available in the context.

 

Ques – How can we call the primary key using java?

Ans – Simple primary keys use the javax.persistence.Id annotation to denote the primary key property or field. Composite primary keys are used when a primary key consists of more than one attribute, which corresponds to a set of single persistent properties or fields.

 

Ques – What is  Spring MVC?

Ans –  The MVC stands for Model-View-Controller, which is its core function, allowing separation of the business logic, presentation logic, and navigation logic. 

Spring MVC is an integrated version of the Spring framework and Model View Controller. It has all the basic features of the core Spring framework like Dependency Injection and Inversion of Control.

 

Ques – Spring core IoC dependency injection

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.

 

Ques – Front end controller

Ans –  In Spring Web MVC, the DispatcherServlet class works as the front controller. It is responsible to manage the flow of the Spring MVC application.

  • Model – A model contains the data of the application. A data can be a single object or a collection of objects.
  • Controller – A controller contains the business logic of an application. Here, the @Controller annotation is used to mark the class as the controller.
  • View – A view represents the provided information in a particular format. Generally, JSP+JSTL is used to create a view page. Although spring also supports other view technologies such as Apache Velocity, Thymeleaf and FreeMarker.

 

Ques –  Modes of autowiring.

Ans –

  • byName: The 
    byName
     mode injects the object dependency according to name of the bean. In such case, property name and bean name should be same. It internally calls setter method.
  • byType: The 
    byType
     mode injects the object dependency according to type. So it can have different property name and bean name. It internally calls setter method.
  • constructor: The 
    constructor
     mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.
  • autodetect: In this mode, Spring first tries to autowire by 
    constructor
    . If this fails, it tries to autowire by using 
    byType

Ques – @transaction annotation  in Spring Data JPA

Ans – The @Transactional annotation tells Spring that a transaction is required to execute this method. When you inject the AuthorService somewhere, Spring generates a proxy object that wraps the AuthorService object and provides the required code to manage the transaction.

 

Ques –   What is DispatcherServlet  in spring?

Ans –  The DispatcherServlet is the front controller in Spring web applications. It’s used to create web applications and REST services in Spring MVC. In a traditional Spring web application, this servlet is defined in the web. xml file.

 

Ques – What is the boiler plate code how can we remove that?

Ans – boilerplate code refers to sections of code that have to be included in many places with little or no alteration.

To remove this we use getters and setters and annotations. You can use the MVC framework to reduce it.

 

Ques –  Spring validation

Ans –  The Spring MVC Validation is used to restrict the input provided by the user. To validate the user’s input, the Spring 4 or higher version supports and use Bean Validation API. It can validate both server-side as well as client-side applications.

Ques – Spring container

Ans – The Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction. The Spring container uses DI to manage the components that make up an application.

 

Ques – ApplicationContext 

Ans –  ApplicationContext is an interface for providing configuration information to an application. There are multiple classes provided by springframework that implements this interface and helps us use configuration information in applications. ApplicationContext provides standard bean factory lifecycle capabilities.

 

Ques –  what is a JPA repository?

Ans – The Java Persistence API (JPA) is the standard way of persisting Java objects into relational databases. The JPA consists of two parts: a mapping subsystem to map classes onto relational tables as well as an EntityManager API to access the objects, define and execute queries, and more.

 

Ques –  what is a rest controller?

Ans – The RestController allows to handle all REST APIs such as GET, POST, Delete, PUT requests. Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model.

 

Ques –  Can we have multiple dispatcher servlet?

Ans – Yes

You can have as many DispatcherServlets as you want. Basically what you need to do is duplicate the configuration and give the servlet a different name (else it will overwrite the previous one), and have some separate configuration classes (or XML files) for it.

Ques – What is data JPA in spring boot?

Ans – JPA is a specification which specifies how to access, manage and persist information/data between java objects and relational databases. It provides a standard approach for ORM, Object Relational Mapping. Spring Boot provides a seemless integration with JPA.

Ques –  Entity manager factory

Ans –The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities. The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit.

Ques –  How to remove boilerplate code using @transectional annotation

Ans – JPA life cycle

Ques –  JPA’s 4 Lifecycle States. 

Ans – The lifecycle model consists of the 4 states transient, managed, removed, and detached. It is same as entity life cycle.

Ques – How to remove boilerplate code using @transectional annotation

Ans – Using @transactional we don’t have to initiate and commit a transaction everytime a method is called

 

Ques –  What is root context in Spring?

Ans –  The root-context in a Spring application is the ApplicationContext that is loaded by the ContextLoaderListener. This context should have globally available resources like services, repositories, infrastructure beans ( DataSource , EntityManagerFactory s etc.) etc.

 

Ques –  What is JDBC?

Ans –  Java™ database connectivity (JDBC) is the JavaSoft specification of a standard application programming interface (API) that allows Java programs to access database management systems. The JDBC API consists of a set of interfaces and classes written in the Java programming language.

 

Ques – How to connect jdbc?

Ans – 

  1. Ensure the JDBC driver you need is on your classpath.
  2. Use the JDBC library to obtain a connection to the database.
  3. Use the connection to issue SQL commands.

 

Ques – Difference between range and size annotation

Ans –  Range is having min and max value, and size is fixed

 

 

 

 

Tags: No tags

Leave A Comment

Your email address will not be published. Required fields are marked *