Top 50 Java & Ejb & Spring Interview Questions You Must Prepare 24.Jun.2024

Q1. What Is Message Driven Bean In Ejb?

A message driven bean is a type of enterprise bean which is invoked by EJB container when it receives a message from queue or topic. Message driven bean is a stateless bean and is used to do task asynchronously.

 

Q2. What Are The Benefits Of Ejb?

Following are the key benefits of EJB.

  • Simplified development of large scale enterprise level application.
  • Application Server/ EJB container provides most of the system level services like traction handling, logging, load balancing, persistence mechanism, exception handling and so on. Developer has to focus only on business logic of the application.
  • EJB container manages life cycle of ejb instances thus developer needs not to worry about when to create/delete ejb objects.

 

Q3. What Is Class Level Interceptor In Ejb?

Class level interceptor is invoked for every method of the bean. Class level interceptor can be applied both by annotation or via xml(ejb-jar.xml).

Q4. What Is A Session Bean In Ejb?

Session bean stores data of a particular user for a single session. It can be stateful or stateless. It is less resource intensive as compared to entity be. Session bean gets destroyed as soon as user session terminates.

Q5. What Is Abstraction?

It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.

Q6. What Are The Benefits Of Ioc?

IOC or dependency injection minimizes the amount of code in an application. It makes easy to test applications, since no singletons or JNDI lookup mechanisms are required in unit tests. Loose coupling is promoted with minimal effort and least intrusive mechanism. IOC containers support eager instantiation and lazy loading of services.

Q7. Explain The Aop Module

The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has been provided by the AOP Alliance in order to ensure the interoperability between Spring and other AOP frameworks. This module also introduces metadata programming to Spring.

Q8. What Are The Common Implementations Of The Applicationcontext?

  • The FileSystemXmlApplicationContext container loads the definitions of the be from an XML file. The full path of the XML bean configuration file must be provided to the constructor.
  • The ClassPathXmlApplicationContext container also loads the definitions of the be from an XML file. Here, you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.
  • The WebXmlApplicationContext: container loads the XML file with definitions of all be from within a web application.

Q9. Explain The Jdbc Abstraction And Dao Module

With the JDBC abstraction and DAO module we can be sure that we keep up the database code clean and simple, and prevent problems that result from a failure to close database resources. It provides a layer of meaningful exceptions on top of the error messages given by several database servers. It also makes use of Spring’s AOP module to provide traction management services for objects in a Spring application.

Q10. Which Types Of Exceptions Are Caught At Compile Time?

Checked exceptions can be caught at the time of program compilation. Checked exceptions must be handled by using try catch block in the code in order to successfully compile the code.

Q11. A Person Says That He Compiled A Java Class Successfully Without Even Having A Main Method In It? Is It Possible?

main method is an entry point of Java class and is required for execution of the program however; a class gets compiled successfully even if it doesn’t have a main method. It can’t be run though.

Q12. Can Variables Be Used In Java Without Initialization?

In Java, if a variable is used in a code without prior initialization by a valid value, program doesn’t compile and gives an error as no default value is assigned to variables in Java.

Q13. When Abstract Methods Are Used?

If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

Q14. What Is Stateless Session Bean In Ejb?

A stateless session bean is a type of enterprise bean which is normally used to do independent operations. A stateless session bean as per its name does not have any associated client state, but it may preserve its instance state. EJB Container normally creates a pool of few stateless bean's objects and use these objects to process client's request. Because of pool, instance variable values are not guaranteed to be same across lookups/method calls.

Q15. What Is An Interface?

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Q16. What Is Spring Configuration File?

Spring configuration file is an XML file. This file contains the classes information and describes how these classes are configured and introduced to each other.

Q17. What Is Stateful Session Bean In Ejb?

A stateful session bean is a type of enterprise bean which preserve the conversational state with client. A stateful session bean as per its name keeps associated client state in its instance variables. EJB Container creates a separate stateful session bean to process client's each request. As soon as request scope is over, statelful session bean is destroyed.

Q18. How Jdbc Can Be Used More Efficiently In Spring Framework?

JDBC can be used more efficiently with the help of a template class provided by spring framework called as JdbcTemplate.

Q19. How Jdbctemplate Can Be Used?

With use of Spring JDBC framework the burden of resource management and error handling is reduced a lot. So it leaves developers to write the statements and queries to get the data to and from the database. JdbcTemplate provides many convenience methods for doing things such as converting database data into primitives or objects, executing prepared and callable statements, and providing custom database error handling.

Q20. Describe Different States Of A Thread.

A thread in Java can be in either of the following states:
Ready: When a thread is created, it’s in Ready state.
Running: A thread currently being executed is in running state.
Waiting: A thread waiting for another thread to free certain resources is in waiting state.
Dead: A thread which has gone dead after execution is in dead state.

Q21. What Is A Traction?

A traction is a single unit of work items which follows the ACID properties. ACID stands for Atomic, Consistent,Isolated and Durable.

Q22. Explain

@javax.ejb.MessageDrivenBean annotation specifies that a given ejb class is a message driven bean. Following are its attributes:
name - Used to specify name of the message driven bean.
messageListenerInterface - Used to specify message listener interface for the message driven bean.
activationConfig - Used to specify the configuration details of the message-driven bean in operational environment of the message driven bean.
mappedName - Used to specify the JNDI name of the message driven bean.
description - Used to provide description of the message driven bean.

Q23. What Is Entity Bean In Ejb?

Entity be represents persistent data storage. User data can be saved to database via entity be and later on can be retrived from the database in the entity bean.

Q24. Can A Dead Thread Be Started Again?

In java, a thread which is in dead state can’t be started again. There is no way to restart a dead thread.

Q25. When Throws Keyword Is Used?

If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature..

Q26. How Can We Use Primitive Data Types As Objects?

Primitive data types like int can be handled as objects by the use of their respective wrapper classes. For example, Integer is a wrapper class for primitive data type int. We can apply different methods to a wrapper class, just like any other object.

Q27. When Super Keyword Is Used?

If the method overrides one of its superclass's methods, overridden method can be invoked through the use of the keyword super. It can be also used to refer to a hidden field.

Q28. Explain

@javax.ejb.Local annotation is used to specify Local interface(s) of a session bean. This local interface states the business methods of the session bean (which can be stateless or stateful).
This interface is used to expose the business methods to local clients which are running in same deployment/application as EJB.
Following are its attributes:
value - Used to specify the list of local interfaces as an array of interfaces.

Q29. Explain

@javax.ejb.Stateless annotation specifies that a given ejb class is a stateless session bean.Following are its attributes:
name - Used to specify name of the session bean.
mappedName - Used to specify the JNDI name of the session bean.
description - Used to provide description of the session bean.

Q30. What Are The Differences Between Stateful Session Bean And Stateless Session Bean?

Following are the differences between stateful session bean and stateless session bean:
EJB Container creates a separate stateful session bean to process client's each request.whereas EJB Container normally creates a pool of few stateless bean's objects and use these objects to process client's request.
As soon as request scope is over, statelful session bean is destroyed but stateless bean remains active.
A stateful session bean is a type of enterprise bean which preserve the conversational state with client. A stateful session bean as per its name keeps associated client state in its instance variables Whereas because of pool of stateless session be, instance variable values are not guaranteed to be same across lookups/method calls in stateless session be.

Q31. Can A Class In Java Be Inherited From More Than One Class?

In Java, a class can be derived from only one class and not from multiple classes. Multiple inheritances is not supported by Java.

Q32. Explain The Object/relational Mapping Integration Module

Spring also supports for using of an object/relational mapping (ORM) tool over straight JDBC by providing the ORM module. Spring provides support to tie into several popular ORM frameworks, including Hibernate, JDO, and iBATIS SQL Maps. Spring’s traction management supports each of these ORM frameworks as well as JDBC.

Q33. Explain The Spring Mvc Module

MVC framework is provided by Spring for building web applications. Spring can easily be integrated with other MVC frameworks, but Spring’s MVC framework is a better choice, since it uses IoC to provide for a clean separation of controller logic from business objects. With Spring MVC you can declaratively bind request parameters to your business objects.

Q34. Which Are The Spring Framework Modules?

The basic modules of the Spring framework are :

  • Core module
  • Bean module
  • Context module
  • Expression Language module
  • JDBC module
  • ORM module
  • OXM module
  • Java Messaging Service(JMS) module
  • Traction module
  • Web module
  • Web-Servlet module
  • Web-Struts module
  • Web-Portlet module

Q35. What Are The Key Components Of Persistence Api In Ejb?

Following are the key components of persistence API in EJB:
Entity - A persistent object representing the data-store record. It is good to be serializable.
EntityManager - Persistence interface to do data operations like add/delete/update/find on persistent object(entity). It also helps to execute queries using Query interface.
Persistence unit (persistence.xml) - Persistence unit describes the properties of persistence mechanism.
Data Source (*ds.xml) - Data Source describes the data-store related properties like connection url. user-name,password etc.

Q36. What Is Default Interceptor In Ejb?

Default interceptor is invoked for every bean within deployment.Default interceptor can be applied only via xml (ejb-jar.xml).

Q37. What Are The Types Of Advice?

Spring aspects can work with five kinds of advice mentioned below:
before: Run advice before the a method execution.
after: Run advice after the a method execution regardless of its outcome.
after-returning: Run advice after the a method execution only if method completes successfully.
after-throwing: Run advice after the a method execution only if method exits by throwing an exception.
around: Run advice before and after the advised method is invoked.

 

Q38. What A Remote Session Bean Is Used In Ejb?

if ejb client is in different environment where ejb session bean is to be deployed then we use remote session bean.

Q39. How Finally Used Under Exception Handling?

The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

Q40. What Is Method Level Interceptor In Ejb?

Method level interceptor is invoked for a particular method of the bean. Method level interceptor can be applied both by annotation of via xml(ejb-jar.xml).

Q41. What Is Abstract Class?

These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.

Q42. Explain

EJB 3.0 provides option to embed JAVA POJO (Plain Old Java Object) into an entity bean and allows to map column names with the methods of the embedded POJO class. A java POJO to be embedded must be annotated as @Embeddable.

Q43. Can We Call A Non-static Method From Inside A Static Method?

Non-Static methods are owned by objects of a class and have object level scope and in order to call the non-Static methods from a static block (like from a static main method), an object of the class needs to be created first. Then using object reference, these methods can be invoked.

 

Q44. Is Message Driven Bean A Stateless Bean?

Message driven bean is a stateless bean and is used to do task asynchronously.

Q45. Which Are The Two Subclasses Under Exception Class?

The Exception class has two main subclasses : IOException class and RuntimeException Class

Q46. Explain The Core Container (application Context) Module?

This is the basic Spring module, which provides the fundamental functionality of the Spring framework. BeanFactory is the heart of any spring-based application. Spring framework was built on the top of this module, which makes the Spring container.

Q47. Can We Use Goto In Java To Go To A Particular Line?

In Java, there is not goto keyword and java doesn’t support this feature of going to a particular labeled line.

 

Q48. What Does A Spring Application Look Like?

An interface that defines the functions.
The implementation that contains properties, its setter and getter methods, functions etc.,Spring AOP 
The Spring configuration XML file.
Client program that uses the function

Q49. When Throw Keyword Is Used?

An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.

Q50. Can A Constructor Have Different Name Than A Class Name In Java?

Constructor in Java must have same name as the class name and if the name is different, it doesn’t act as a constructor and compiler thinks of it as a normal method.