JAVA/J2EE Interview Questions Part 2

Here you will find  the continuous of 50-100 Java interview questions . What’s being presented here are some commonly asked Java interview questions that cover a broad range of topics in Java.We are sharing 50 java interview questions , these questions are frequently asked by the recruiters.

These Java interview questions are mix of easy, tough and tricky Java questions e.g. Why multiple inheritance is not supported in Java is one of the tricky question in java. The answers of the java interview questions are short and to the point. Most questions are asked on Senior and experienced level i.e. 3, 4, 5 or 6 years of Java experience. If there is any java interview questions that have been asked to you, kindly post it in the comment section. We assure that you will get here the 90% frequently asked java interview questions.

51) What are considered as a web component?

Java Servlet and Java Server Pages technology components are web components. Servlets are Java programming language that dynamically receive requests and make responses. JSP pages execute as servlets but allow a more natural approach to creating static content.

52) Define Hash table?             

Hash Table is just like Hash Map or Collection having unique key value pairs. Hash table is a collection of Synchronized object. It does not allow duplicate values but it allows null values.

53) What is Hibernate?

Hibernate is a open source object-relational mapping and query service. In hibernate we can write HQL instead of SQL which save developers to spend more time on writing the native SQL. Hibernate has more powerful association, inheritance, polymorphism, composition, and collections. It is a beautiful approach for persisting into database using the java objects. Hibernate also allows you to express queries using java-based criteria.

54) What is ORM?

ORM stands for Object-Relational mapping. The objects in a Java class which is mapped in to the tables of a relational database using the metadata that describes the mapping between the objects and the database. It works by transforming the data from one representation to another.

55) Difference between Proxy and Adapter?

Adapter object has a different input than the real subject whereas Proxy object has the same input as the real subject. Proxy object is such that it should be placed as it is in place of the real subject.

56) What is MVC?

Model-View-Controller is a design pattern wherein Model holds the business logic , Controller controls the flow and View takes care of the presentation part of web application.

57) What is dependency injection or IOC?

It’s a mechanism of passing object creation to an external component wherein dependent objects are identified and created using configuration. The Spring container is able to auto wire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory without using <constructor-arg> and <property> element.

59) What are the different types of IOC (dependency injection)?

Constructor Injection (e.g. Pico container, Spring etc): Dependencies are provided as constructor parameters. Setter Injection (e.g. Spring): Dependencies are assigned through JavaBeans properties (ex: setter methods). Interface Injection (e.g. Avalon): Injection is done through an interface. Note: Spring supports only Constructor and Setter Injection

60) What are the advantages of Spring framework?

The advantages of Spring are as follows: Spring has layered architecture. Use what you need and leave you don’t need now. Spring Enables POJO Programming. There is no behind the scene magic here. POJO programming enables continuous integration and testability. Dependency Injection and Inversion of Control Simplifies JDBC Open source and no vendor lock-in.

61) What are the common implementations of the Application Context?

The three commonly used implementation of ‘Application Context’ are, ClassPathXmlApplicationContext : It Loads context definition from an XML file located in the classpath, treating context definitions as classpath resources. The application context is loaded from the application’s classpath by using the code. ApplicationContext context = new ClassPathXmlApplicationContext(“bean.xml”); FileSystemXmlApplicationContext : It loads context definition from an XML file in the filesystem. The application context is loaded from the file system by using the code. ApplicationContext context = new FileSystemXmlApplicationContext(“bean.xml”); XmlWebApplicationContext : It loads context definition from an XML file contained within a web application.

62) What is DelegatingVariableResolver?

Spring provides a custom JavaServer Faces VariableResolver implementation that extends the standard Java Server Faces managed beans mechanism which lets you use JSF and Spring together. This variable resolver is called as DelegatingVariableResolver.

63) What is Significance of JSF- Spring integration?

Spring – JSF integration is useful when an event handler wishes to explicitly invoke the bean factory to create beans on demand, such as a bean that encapsulates the business logic to be performed when a submit button is pressed.

64) What are the ways to access Hibernate using Spring?

There are two approaches to Spring’s Hibernate integration: Inversion of Control with a HibernateTemplate and Callback Extending HibernateDaoSupport and Applying an AOP Interceptor.

65) How the AOP used in Spring?

AOP is used in the Spring Framework: To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management, which builds on the Spring Framework’s transaction abstraction. To allow users to implement custom aspects, complementing their use of OOP with AOP.

66) What is Session Tracking?

Session simply means a particular interval of time. Session Tracking is a way to maintain state of an user. Http protocol is a stateless protocol. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of a user to recognize to particular user.

67) How is a typical spring implementation look like?

For a typical Spring Application we need the following files: An interface that defines the functions. An Implementation that contains properties, its setter and getter methods, functions etc., Spring AOP (Aspect Oriented Programming) A XML file called spring configuration file. Client program that uses the function.

68) What is Java Server Faces (JSF) – spring integration mechanism?

Spring provides a custom JavaServer Faces VariableResolver implementation that extends the standard JavaServer Faces managed beans mechanism. When asked to resolve a variable name, the following algorithm is performed.

69) What is Significance of JSF- Spring integration?

Spring – JSF integration is useful when an event handler wishes to explicitly invoke the bean factory to create beans on demand, such as a bean that encapsulates the business logic to be performed when a submit button is pressed.

70) How to integrate your Struts application with spring?

To integrate your Struts application with spring, we have two options: Configure spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependencies in a spring context file. Subclass Spring’s ActionSupport classes and grab your Spring-managed beans explicitly using a getWebApplicationContext() method.

71) How to integrate spring and Hibernate?

Spring and Hibernate can integrate using spring’s SessionFactory called LocalSessionFactory. The integration process is of 3 steps. Configure Hibernate mappings. Configure Hibernate properties. Wire dependant object to SessionFactory.

72) What are the ways to access Hibernate using Spring?

There are two ways to access Hibernate from Spring: Through Hibernate Template. Subclassing HibernateDaoSupport Extending HibernateDaoSupport and Applying an AOP Interceptor

73) Why most users of the Spring Framework choose declarative transaction management?

Most users of the Spring Framework choose declarative transaction management because it is the option with the least impact on application code, and hence is most consistent with the ideals of a non-invasive lightweight container.

74) When to use programmatic and declarative transaction management?

Programmatic transaction management is usually a good idea only if you have a small number of transactional operations. On the other hand, if your application has numerous transactional operations, declarative transaction management is usually worthwhile. It keeps transaction management out of business logic, and is not difficult to configure.

75)Explain about the Spring DAO support?

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows one to switch between the persistence technologies fairly easily and it also allows one to code without worrying about catching exceptions that are specific to each technology.

76) What are the exceptions thrown by the Spring DAO classes?

Spring DAO classes throw exceptions which are subclasses of DataAccessException(org.springframework.dao.DataAccessException).Spring provides a convenient translation from technology-specific exceptions like SQLException to its own exception class hierarchy with the DataAccess Exception as the root exception. These exceptions wrap the original exception.

77) What is SQLExceptionTranslator?

SQLExceptionTranslator, is an interface to be implemented by classes that can translate between SQLExceptions and Spring’s own data-access-strategy-agnostic org.springframework.dao.DataAccessException.

78) What is Spring’s JdbcTemplate ?

Spring’s JdbcTemplate is central class to interact with a database through JDBC. 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.

JdbcTemplate template = new JdbcTemplate(myDataSource);

78) What is PreparedStatementCreator ?

PreparedStatementCreator: Is one of the most commonly used interfaces for writing data to database. Has one method – createPreparedStatement(Connection) Responsible for creating a PreparedStatement. Does not need to handle SQLExceptions.

79) What is Application Context?

A bean factory is fine to simple applications, but to take advantage of the full power of the Spring framework, you may want to move up to Springs more advanced container, the application context. On the surface, an application context is same as a bean factory.Both load bean definitions, wire beans together, and dispense beans upon request. But it also provides:

* A means for resolving text messages, including support for internationalization.

* A generic way to load file resources.

* Events to beans that are registered as listeners.

80) What are the types of Dependency Injection Spring supports?

Setter Injection: Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean. Constructor Injection: Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.

81) What are the exceptions thrown by the Spring DAO classes ?

Spring DAO classes throw exceptions which are subclasses of DataAccessException(org.springframework.dao.DataAccessException).Spring provides a convenient translation from technology-specific exceptions like SQLException to its own exception class hierarchy with the DataAccessException as the root exception. These exceptions wrap the original exception.

82) What is Spring’s JdbcTemplate ?

Spring’s JdbcTemplate is central class to interact with a database through JDBC. 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. JdbcTemplate template = new JdbcTemplate (myDataSource);

83)Explain the three different methods which are provided by the Namespace Handler interface?

The three different methods which are provided by the name space handler interface are:

1) Init() is used to allow initialization for name space handler.

2) BeanDefinition parse is used when a Spring encounters a top level element.

3) BeanDefinitionHandler is used when spring encounters an attribute.

84) Which of the beans available in EJB 3 are only called by clients ?

Session and Singleton (EJB 3.1) beans are the ones who encapsulate the business logic which is why it is the only bean which is called by clients. Other beans like MDB and Entity beans are called by session and singleton beans to complete the business requirement.

85) Why is it so to make the session beans into two parts one with interface and the other as implementation to the interface? Or why session beans should be used through business interface? (EJB 3.0 Specific).

This approach promotes loose coupling since implementation classes can easily be swapped out without a lot of code changes. But in recent EJB 3.1 specification you don’t need to implement a business interface for faster development.

86) What is business interface?

An interface through which a client invokes the bean is called a business interface. There are three types of business interfaces available in EJB 3 : Local, Remote and WebService.

87) Can we have instance fields in Stateless session bean?

Yes. A stateless session bean can have instance field, BUT not for storing state information as opposed to Stateful beans. There may be a need of storing certain shareable resources like data source, JMS source etc, which will be used in the exposed method. So it is perfectly safe to have instance fields in the Stateless session beans.

88) Can you inject Stateful beans into stateless session bean or Servlet?

Yes!!. Nobody stops you to inject Stateful beans into stateless session bean or a Servlet. Let’s take a Servlet and inject (definitely as a field in Servlet) a Stateful bean; What happens? Container creates a single instance of a Servlet, so the instance field of Servlet ( which is a Stateful Bean ) injected , is shared among clients.It will lead to drastic data inconsistency ( stored in Stateful bean ) being shared by clients. And this is no different for a Stateful bean injected into stateless bean. So you must AVOID injecting into stateless beans and Servlets. Please don’t ask how do you access or make use of Stateful bean in Stateless or Servlet.The answer is JNDI inside the methods of Stateless and doGet() or doPost() of Servlet.

89) What are Pull-MVC and push-MVC based architecture ? Whicharchitecture does Struts2 follow?

Pull-MVC and Push-MVC are better understood with how the view layer is getting data i.e. Model to render. In case of Push-MVC the data( Model) is constructed and given to the view layer by the Controllers by putting it in the scoped variables like request or session. Typical example is Spring MVC and Struts1. Pull-MVC on the other hand puts the model data typically constructed in Controllers are kept in a common place i.e. in actions, which then gets rendered by view layer. Struts2 is a Pull-MVC based architecture, in which all data is stored in Value Stack and retrieved by view layer for rendering.

90) Are Interceptors in Struts2 thread safe?

No, Unlike Struts2 action, Interceptors are shared between requests, so thread issues will come if not taken care of.

91) Are Interceptors and Filters different? , If yes then how?

Apart from the fact that both Interceptors and filters are based on intercepting filter,there are few differences when it comes to Struts2.

Filters:(1)Based on Servlet Specification

(2)Executes on the pattern matches on the request.(3) Not configurable method calls

Interceptors: (1)Based on Struts2.

(2)Executes for all the request qualifies for a front controller( A Servlet filter ).And can be configured to execute additional interceptor for a particular action execution.

(3)Methods in the Interceptors can be configured whether to execute or not by means of excludemethods or includeMethods. ( see tutorial on this Controlling Interceptor Behavior)

92) Which class is the front-controller in Struts2?

The class “org.apache.struts2.dispatcher.FilterDispatcher ” is the front controller in Struts2. In recent time Struts 2.1.3 this class is deprecated and new classes are introduced to do the job.

93) What is the role of Action/ Model?

Actions in Struts are POJO, is also considered as a Model. The role of Action are to execute business logic or delegate call to business logic by the means of action methods which is mapped to request and contains business data to be used by the view layer by means of setters and getters inside the Action class and finally helps the framework decide which result to render.

94) What is the relation between ValueStack and OGNL?

A ValueStack is a place where all the data related to action and the action itself is stored. OGNL is a mean through which the data in the ValueStack is manipulated.

95) What is the difference between Action and ActionSupport?

Action is interface defines some string like SUCCESS,ERROR etc and an execute() method. For convenience Developer implement this interface to have access to String field in action methods. ActionSupport on other hand implements Action and some other interfaces and provides some feature like data validation and localized error messaging when extended in the action classes by developers

96) Who loads the struts.xml file? Which Struts2 API loads the struts.xml file?

In Struts2 FilterServlet is the responsible class for loading struts.xml file as we deploy the application on the container. Unlike Servlet (as with Struts1) needs the load-on-startup tag to load the front controller,a filter doesn’t need to have load on startup tag to be loaded as the application is deployed. As with servlet specification a filter is loaded/executed as the application starts up.

97) What is JDBC Driver interface?

The JDBC Driver interface provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendor driver must provide implementations of the java.sql.Connection, Statement, PreparedStatement, CallableStatement, ResultSet and Driver.

98) What is the jspInit() method?

The jspInit() method of the javax.servlet.jsp.JspPage interface is similar to the init() method of servlets. This method is invoked by the container only once when a JSP page is initialized. It can be overridden by a page author to initialize resources such as database and network connections, and to allow a JSP page to read persistent configuration data.

99) What is the _jspService() method?

The _jspService() method of the javax.servlet.jsp.HttpJspPage interface is invoked every time a new request comes to a JSP page. This method takes the HttpServletRequest and HttpServletResponse objects as its arguments. A page author cannot override this method, as its implementation is provided by the container.

100) What is the jspDestroy() method?

The jspDestroy() method of the javax.servlet.jsp.JspPage interface is invoked by the container when a JSP page is about to be destroyed. This method is similar to the destroy() method of servlets. It can be overridden by a page author to perform any cleanup operation such as closing a database connection.

One thought on “JAVA/J2EE Interview Questions Part 2

  1. Valson

    Good quality and quantity of java interview questions….. Thank u…
    Can you please share a list of frequently asked Java programs on Interviews as well?

    Reply

Enter Your Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>