Java & EJB & Spring Interview Questions & Answers

4 avg. rating (80% score) - 1 votes

Java & EJB & Spring Interview Questions & Answers

If you are a Java programming Expert then this is the right opportunity for you. Let us know if you looking for a job change? Are you prepared in attending an interview? Then do not worry, we’ve a right answer for your job interview preparation. If you are preparing for Java & EJB & Spring job interview and don’t know how to crack interview and what level or difficulty of questions to be asked in job interviews then go through Wisdomjobs interview questions and answers page to crack your job interview. Spring is developed as an alternate to EJB and both comes under Java. Below are the frequently asked Java & EJB & Spring job interview questions and answers which makes you comfortable to face the interviews:

Java & EJB & Spring Interview Questions

Java & EJB & Spring Interview Questions
    1. Question 1. What Is An Interface?

      Answer :

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

    2. Question 2. Give Some Features Of Interface?

      Answer :

      • Interface cannot be instantiated
      • An interface does not contain any constructors.
      • All of the methods in an interface are abstract.

    3. Question 3. Which Are The Two Subclasses Under Exception Class?

      Answer :

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

    4. Question 4. When Throws Keyword Is Used?

      Answer :

      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..

    5. Question 5. When Throw Keyword Is Used?

      Answer :

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

    6. Question 6. How Finally Used Under Exception Handling?

      Answer :

      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.

    7. Question 7. What Things Should Be Kept In Mind While Creating Your Own Exceptions In Java?

      Answer :

      While creating your own exception −

      • All exceptions must be a child of Throwable.
      • If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.
      • You want to write a runtime exception, you need to extend the RuntimeException class.

    8. Question 8. Define Inheritance?

      Answer :

      It is the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.

    9. Question 9. When Super Keyword Is Used?

      Answer :

      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.

    10. Question 10. What Is Polymorphism?

      Answer :

      Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

    11. Question 11. What Is Abstraction?

      Answer :

      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.

    12. Question 12. What Is Abstract Class?

      Answer :

      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.

    13. Question 13. When Abstract Methods Are Used?

      Answer :

      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.

    14. Question 14. Is Jdk Required On Each Machine To Run A Java Program?

      Answer :

      JDK is development Kit of Java and is required for development only and to run a Java program on a machine, JDK isn’t required. Only JRE is required.

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

      Answer :

      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.

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

      Answer :

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

       

    17. Question 17. Can A Dead Thread Be Started Again?

      Answer :

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

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

      Answer :

      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.

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

      Answer :

      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.

       

    20. Question 20. What Are The Two Environment Variables That Must Be Set In Order To Run Any Java Programs?

      Answer :

       Java programs can be executed in a machine only once following two environment variables have been properly set:

      1. PATH variable
      2. CLASSPATH variable

    21. Question 21. Can Variables Be Used In Java Without Initialization?

      Answer :

      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.

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

      Answer :

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

    23. Question 23. How Can We Use Primitive Data Types As Objects?

      Answer :

      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.

    24. Question 24. Which Types Of Exceptions Are Caught At Compile Time?

      Answer :

      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.

    25. Question 25. Describe Different States Of A Thread.

      Answer :

      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.

    26. Question 26. What Is Ejb?

      Answer :

      EJB stands for Enterprise Java Beans. EJB is an essential part of a J2EE platform. J2EE platform have component based architecture to provide multi-tiered, distributed and highly transactional features to enterprise level applications.
      EJB provides an architecture to develop and deploy component based enterprise applications considering robustness, high scalability and high performance. An EJB application can be deployed on any of the application server compliant with J2EE 1.3 standard specification.

    27. Question 27. What Are The Benefits Of Ejb?

      Answer :

      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 transaction 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.

       

    28. Question 28. What Is A Session Bean In Ejb?

      Answer :

      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 beans. Session bean gets destroyed as soon as user session terminates.

    29. Question 29. What Is Stateful Session Bean In Ejb?

      Answer :

      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.

    30. Question 30. What Is Stateless Session Bean In Ejb?

      Answer :

      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.

    31. Question 31. What Is Entity Bean In Ejb?

      Answer :

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

    32. Question 32. What Is Message Driven Bean In Ejb?

      Answer :

      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.

       

    33. Question 33. When A Local Session Bean Is Used In Ejb?

      Answer :

      If ejb client is in same environment where ejb session bean is to be deployed then we use local session bean.

       

    34. Question 34. What A Remote Session Bean Is Used In Ejb?

      Answer :

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

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

      Answer :

      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 beans, instance variable values are not guaranteed to be same across lookups/method calls in stateless session beans.

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

      Answer :

      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.

    37. Question 37. Is Message Driven Bean A Stateless Bean?

      Answer :

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

    38. Question 38. Explain @javax.ejb.stateless Annotation.?

      Answer :

      @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.

    39. Question 39. Explain @javax.ejb.stateful Annotation.?

      Answer :

      @javax.ejb.Stateful annotation specifies that a given ejb class is a stateful 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.

    40. Question 40. Explain @javax.ejb.messagedrivenbean Annotation.

      Answer :

      @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.

    41. Question 41. Explain @javax.ejb.ejb Annotation.

      Answer :

      @javax.ejb.EJB annotation is used to specify or inject a dependency as ejb instance into another ejb. Following are its attributes:
      name - Used to specify name which will be used to locate the referenced bean in environment.
      beanInterface - Used to specify the interface type of the referenced bean.
      beanName - Used to provide name of the referenced bean.
      mappedName - Used to specify the JNDI name of the referenced bean.
      description - Used to provide description of the referenced bean.

    42. Question 42. Explain @javax.ejb.local Annotation.

      Answer :

      @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.

    43. Question 43. What Is Container Managed Transactions?

      Answer :

      In this type, container manages the transaction states.

    44. Question 44. What Is A Transaction?

      Answer :

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

    45. Question 45. Which Types Of Java Classes Can Be Mapped Using @lob Annotation?

      Answer :

      Following java types can be mapped using @Lob annotation:

      • java.sql.Blob
      • java.sql.Clob
      • byte[]
      • String
      • Serializable Object

    46. Question 46. Explain @lob Annotation.

      Answer :

      EJB 3.0 provides support for Blob and Clob types using @Lob annotation.

    47. Question 47. Explain @embeddable Annotation.

      Answer :

      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.

    48. Question 48. What Is Method Level Interceptor In Ejb?

      Answer :

      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).

    49. Question 49. What Is Default Interceptor In Ejb?

      Answer :

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

    50. Question 50. What Is Class Level Interceptor In Ejb?

      Answer :

      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).

    51. Question 51. What Is Spring?

      Answer :

      Spring is an open source development framework for Enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make Java EE development easier to use and promote good programming practice by enabling a POJO-based programming model.

    52. Question 52. What Are Benefits Of Spring Framework?

      Answer :

      Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.
      Inversion of control (IOC): Loose coupling is achieved in Spring, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects.
      Aspect oriented (AOP): Spring supports Aspect oriented programming and separates application business logic from system services.
      Container: Spring contains and manages the life cycle and configuration of application objects.
      MVC Framework: Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks.
      Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA).
      Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.

    53. Question 53. Which Are The Spring Framework Modules?

      Answer :

      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
      • Transaction module
      • Web module
      • Web-Servlet module
      • Web-Struts module
      • Web-Portlet module

    54. Question 54. Explain The Core Container (application Context) Module?

      Answer :

      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.

    55. Question 55. What Is Beanfactory?

      Answer :

      A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.
      The most commonly used BeanFactory implementation is the XmlBeanFactory class.

    56. Question 56. Explain Xmlbeanfactory?

      Answer :

      The most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.

    57. Question 57. Explain The Aop Module

      Answer :

      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.

    58. Question 58. Explain The Jdbc Abstraction And Dao Module

      Answer :

      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 transaction management services for objects in a Spring application.

    59. Question 59. Explain The Object/relational Mapping Integration Module

      Answer :

      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 transaction management supports each of these ORM frameworks as well as JDBC.

    60. Question 60. Explain The Web Module

      Answer :

      The Spring web module is built on the application context module, providing a context that is appropriate for web-based applications. This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects. It also contains integration support with Jakarta Struts.

    61. Question 61. Explain The Spring Mvc Module

      Answer :

      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.

    62. Question 62. What Is Spring Configuration File?

      Answer :

      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.

    63. Question 63. What Is Spring Ioc Container?

      Answer :

      The Spring IoC is responsible for creating the objects,managing them (with dependency injection (DI)), wiring them together, configuring them, as also managing their complete lifecycle.

    64. Question 64. What Are The Benefits Of Ioc?

      Answer :

      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.

    65. Question 65. What Are The Common Implementations Of The Applicationcontext?

      Answer :

      • The FileSystemXmlApplicationContext container loads the definitions of the beans 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 beans 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 beans from within a web application.

    66. Question 66. What Is The Difference Between Bean Factory And Applicationcontext?

      Answer :

      Application contexts provide a means for resolving text messages, a generic way to load file resources (such as images), they can publish events to beans that are registered as listeners. In addition, operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context. The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable.

    67. Question 67. What Does A Spring Application Look Like?

      Answer :

      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

    68. Question 68. How Jdbctemplate Can Be Used?

      Answer :

      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.

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

      Answer :

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

    70. Question 70. What Are The Types Of The Transaction Management Spring Supports?

      Answer :

      Spring supports two types of transaction management:
      Programmatic transaction management: This means that you have managed the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.
      Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration

    71. Question 71. What Are The Types Of Advice?

      Answer :

      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.

       

    72. Question 72. What Are The Different Points Where Weaving Can Be Applied?

      Answer :

      Weaving can be done at compile time, load time, or at runtime.

    73. Question 73. What Is Target Object?

      Answer :

      The object being advised by one or more aspects, this object will always be a proxy object. Also referred to as the advised object.

       

    74. Question 74. Describe Some Of The Standard Spring Events.

      Answer :

      Spring provides the following standard events:

      ContextRefreshedEvent: This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh method on the ConfigurableApplicationContext interface.
      ContextStartedEvent: This event is published when the ApplicationContext is started using the start method on the ConfigurableApplicationContext interface. You can poll your database or you can re/start any stopped application after receiving this event.
      ContextStoppedEvent: This event is published when the ApplicationContext is stopped using the stop method on the ConfigurableApplicationContext interface. You can do required housekeep work after receiving this event.
      ContextClosedEvent: This event is published when the ApplicationContext is closed using the close method on the ConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted.
      RequestHandledEvent: This is a web-specific event telling all beans that an HTTP request has been serviced.

       

    75. Question 75. What Is The Difference Between Concern And Cross-cutting Concern In Spring Aop?

      Answer :

      Concern: Concern is behavior which we want to have in a module of an application. Concern may be defined as a functionality we want to implement. Issues in which we are interested define our concerns.
      Cross-cutting concern: It's a concern which is applicable throughout the application and it affects the entire application. e.g. logging , security and data transfer are the concerns which are needed in almost every module of an application, hence are cross-cutting concerns.

       

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

EJB(Enterprise JavaBeans) Tutorial