101 Most Important Frequently Asked Advance Java Interview Questions – Part 2

In continuation of our previous article 101 Core Java Interview Questions to Ace the Interview, this is the second part where we cover Advance Java Interview Questions.

Read first part – 101 Most Important Frequently Asked Advance Java Interview Questions -Part1

Advance Java Interview Questions

Q.1  What are the Advantages of JSP over Servlet?

1) Extension to Servlet

JSP technology is the extension to servlet technology. We can use all the features of a servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP development easy.

2) Easy to maintain

JSP can be easily managed because we can easily separate our business logic with presentation logic. In servlet technology, we mix our business logic with the presentation logic.

3) Fast Development: No need to recompile and redeploy

If JSP page is modified, we don’t need to recompile and redeploy the project. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application.

4) Less code than Servlet

In JSP, we can use a lot of tags such as action tags, JSTL, custom tags etc. that reduces the code. JSP does not require additional files like java class files, web.xml etc

5) JSP is built on Java technology, so it is platform independent.

Q.2 What are the advantages of using JSP over other technologies?

JSP vs. Active Server Pages (ASP): The dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.

JSP vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML.

JSP vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for “real” programs that use form data, make database connections, and the like.

JSP vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.

JSP vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.

Q.3 What are the JSP Life Cycle phases?

 Translation phase: in this JSP pages get translated to servlet code. This is done when the first request to the JSP page is made.

JSP Compilation: When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page.

JSP Initialization: it is performed only once and as with the servlet init method, you generally initialize database connections, open files in the jspInit method.

JSP Execution: Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.

JSP Cleanup:

The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.

The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files.

Q.4 Which are the Implicit objects in JSP?

JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.

JSP provide access to some implicit object which represents some commonly used objects for servlets that JSP page developers might need to use. For example, you can retrieve HTML form parameter data by using request variable, which represents the HttpServletRequest object.

There are 9 JSP implicit objects. These objects are created by the web container that is available to all the JSP pages.

1) out – The JspWriter object associated with the o/p stream of response.

2) request – The HttpServletRequest object associated with a request

3) response – The HttpServletResponse object associated with response

4) config – ServletConfig object associated with the servlet for current JSP page.

5) session – HttpSession object associated with the session for the given user of a request

6) application – ServletContext object for web application

7) pageContext – the PageContext object that encapsulates the environment of a single request for this current JSP page.

8) page – The page variable is equivalent to this variable in java

9) exception – The exception object represents a Throwable object that was thrown by some other JSP page.

Q.5 What is The Scriptlet?

A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language.

  • Scriptlet Tag allows you to write java code inside JSP page.
  • Scriptlet tag implements the _jspService method functionality by writing script/java code. Syntax of Scriptlet Tag is as follows :

<% java code %>

  • Everything written inside the scriptlet tag is compiled as java code. Like in the above example, we initialize count variable of type int with a value of 0. And then we print it while using ++ operator to perform addition on it.
  • JSP makes it so easy to perform calculations, database interactions etc directly from inside the HTML code. Just write your java code inside the scriptlet tags

Q.6  What is JSP expression tag?

  • The code placed within JSP expression tag is written to the output stream of the response. So you need not write out.print() to write data. It is mainly used to print the values of variable or method. you cannot use a semicolon to end an expression.

 Syntax of JSP expression tag <%=  statement %>

  • Example Current Time: <%= java.util.Calendar.getInstance().getTime() %>
  • Today’s date: <%= (new java.util.Date()).toLocaleString()%>

Q.7  What is JSP Declarations?

A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file.

Following is the syntax of JSP Declarations:

<%! declaration; [ declaration; ]+ … %>

Example :

<%! int i = 0; %>      <%! int a, b, c; %>

<%! Circle a = new Circle(2.0); %>

Q.8  When to use Declaration tag and not scriptlet tag?

If you want to include any method in your JSP file, then you must use the declaration tag, because during translation phase of JSP, methods, and variables inside the declaration tag, become instance methods and instance variables and are also assigned default values.

Q.9  What is Directive Tag?

Directive Tag gives special instruction to Web Container at the time of page translation. Directive tags are of three types: page, include and taglib.

  • Page directive: The Page directive defines a number of page-dependent properties which communicates with the Web Container at the time of translation. This directive is used for setting up the attributes of a JSP page.
    Syntax:
    <%@ page attribute=”value”%>

2) Include directive: Include a JSP file into another JSP file during the translation phase of JSP life cycle.

Syntax:
<%@ include attribute=”value”%>

3) Taglib directive: This directive is basically used for custom tags.

Q.10  Which are the Page directive attributes.

  • language
  • extends
  • import
  • session
  • isThreadSafe
  • info
  • errorPage
  • isErrorpage
  • contentType
  • isELIgnored
  • buffer
  • autoFlush
  • isScriptingEnabled

Q.11  How do you add a comment on a JSP page?

This is how we can do it: <%– JSP comment –%>

Q.12  Write an implicit object which is not available in normal JSP pages?

JSP exception implicit object is not available in normal JSP pages and it’s used only in JSP error pages to catch the exception thrown by the JSP pages and provide some useful message to the client.

Q.13  Which are the ways to perform exception handling in JSP.

  • An exception is normally an object that is thrown at runtime.
  • Exception Handling is the process to handle the runtime errors.
  • There may occur exception any time in your web application. So handling exceptions is a safer side for the web developer.
  • In JSP, there are two ways to perform exception handling: By errorPage and isErrorPage attributes of the page directive

Q.14  Explain implicit object: response.

In JSP, the response is an implicit object of type HttpServletResponse. The instance of HttpServletResponse is created by the web container for each jsp request.

It can be used to add or manipulate response such as redirect response to another resource, send error etc.

Ex:using response implicit object, we are redirecting the response to the Google.

index.html

welcome.jsp

Q.15  Explain implicit object : request.

The JSP request is an implicit object of type HttpServletRequest i.e. created for each jsp request by the web container.

It can be used to get request information such as parameter, header information, remote address, server name, server port, content type, character encoding etc.

It can also be used to set, get and remove attributes from the jsp request scope.

index.html

welcome.jsp

Q.16  Explain implicit object: config.

In JSP, config is an implicit object of type ServletConfig. This object can be used to get initialization parameter for a particular JSP page. The config object is created by the web container for each jsp page.

Generally, it is used to get initialization parameter from the web.xml file.

Q.17  Explain pageContext implicit object

In JSP, pageContext is an implicit object of type PageContext class.

The pageContext object can be used to set,get or remove attribute from one of the following scopes:

  • page
  • request
  • session
  • application

Q.18  Explain implicit object: application.

In JSP, application is an implicit object of type ServletContext.

The instance of ServletContext is created only once by the web container when application or project is deployed on the server.

This object can be used to get initialization parameter from configuaration file (web.xml). It can also be used to get, set or remove attribute from the application scope.

This initialization parameter can be used by all jsp pages.

Q.19  How to Invoke a Servlet from a JSP Page.

As when invoking one JSP page from another, you can invoke a servlet from a JSP page through the jsp:include and jsp:forward action tags

When this statement is encountered during page execution, the page buffer is output to the browser and the servlet is executed. When the servlet has finished executing, control is transferred back to the JSP page and the page continues executing.

This is the same functionality as for jsp:include actions from one JSP page to another.

  • And as with jsp:forward actions from one JSP page to another, the following statement would clear the page buffer, terminate the execution of the JSP page, and execute the servlet:

Q.20  How to Invoking a JSP Page from a Servlet?

You can invoke a JSP page from a servlet through functionality of the standard javax.servlet.RequestDispatcher interface.

use following steps in your code to use this mechanism:

Get a servlet context instance from the servlet instance:

Get a request dispatcher from the servlet context instance, specifying the page-relative or application-relative path of the target JSP page as input to the getRequestDispatcher() method:

Prior to or during this step, you can optionally make data available to the JSP page through attributes of the HTTP request object.

Invoke the include() or forward() method of the request dispatcher, specifying the HTTP request and response objects as arguments.

For example:  rd.include(request, response); or:

rd.forward(request, response);

The functionality of these methods is similar to that of jsp:include and jsp:forward actions.

The include() method only temporarily transfers control; execution returns to the invoking servlet afterward.

the forward() method clears the output buffer.

Q.21  Which JSP lifecycle methods can you override in your JSP application?

we can only override jspInit() and jspDestroy(), we cannot override the _jspService() method within a JSP page. By overriding jspInit() method you can initialize things like database connections, network connections etc. Whatever you initialize in jspInit() method can be released in jspDestroy() method.

Q.22  What is buffer attribute in JSP and what happens if it sets to value none?

The buffer attribute specifies buffering characteristics for the server output response object. When it is set to “none”, servlet output is immediately directed to the response output object.

Q.23  How to set cookies in JSP?

It involves three steps −

  • Creating a Cookie object − You call the Cookie constructor with a cookie name and a cookie value, both of which are strings.
  • Setting the maximum age − You use setMaxAge to specify how long (in seconds) the cookie should be valid.
  • Sending the Cookie into the HTTP response headers − You use response.addCookie to add cookies in the HTTP response header

Q.24  How to read cookies in JSP?

To read cookies in jsp,  create an array of javax.servlet.http.Cookie objects by calling the getCookies( ) method of HttpServletRequest.

Then  use getName() and getValue() methods to access each cookie and associated value.

Q.25  How to delete cookies in JSP?

To delete cookies is very simple. If you want to delete a cookie then you simply need to follow up following three steps −

  1. Read an already existing cookie and store it in Cookie object.
  2. Set cookie age as zero using setMaxAge() method to delete an existing cookie.
  3. Add this cookie back into response header.

Q.26  How do you connect to a URL resource in Java programming language?

The Java API provides the ‘URLConnecton’ class which can be used to create a connection to a URL. If you have a URL object, you can get the URLConnection object by calling openConnection() method on the URL object. Once you have the URLConnection object you can connect to the URL resource by calling the connect() method on the URLConnection object. You can use the URLRequest object to setup parameters and properties that you may need for making the URL connection.

Q.27  What is URL?

A URL (Uniform Resource Locator) is a unique identifier for any resource located on the Internet. The syntax for URL is given as:

<protocol>://<hostname>[:<port>][/<pathname>][/<filename>[#<section>]]

Q.28  What isMAC Address?

 Each machine is uniquely identified by a physical address, address of the network interface card.

It is 48-bit address represented as 12 hexadecimal characters:

For Example: 00:09:5B:EC:EE:F2

Q.29  What is IP Address?

It is used to uniquely identify a network and a machine in the network. It is referred as global addressing scheme. Also called as logical address.

Currently used type of IP addresses is: Ipv4 – 32-bit address and Ipv6 – 128-bit address.

For Example: Ipv4 – 192.168.16.1

Ipv6 – 0001:0BA0:01E0:D001:0000:0000:D0F0:0010

Q.30  What is Port Address?

It is the address used in the network to identify the application on the network.

Q.31  What is Domain Name Service (DNS)?

It is very difficult to remember the IP addresses of machines in a network. Instead, we can identify a machine using a “domain name” which is character based naming mechanism.

Example: www.google.com.

The mapping between the domain name and the IP address is done by a service called DNS.

Q.32  Which are the protocols used in the Internet ?

  1. IP (Internet Protocol): is a network layer protocol that breaks data into small packets and routes them using IP addresses.
  2. TCP (Transmission Control Protocol): This protocol is used for connection-oriented communication between two applications on two different machines. It is most reliable and implements a connection as a stream of bytes from source to destination.
  3. UDP (User Datagram Protocol): It is a connectionless protocol and used typically for a request and reply services.

Q.33  What is Sockets?

A socket represents the end-point of the network communication. It provides a simple read/write interface and hides the implementation details network and transport layer protocols.

It is used to indicate one of the two end-points of a communication link between two processes.

When a client wishes to make a connection to a server, it will create a socket at its end of the communication link.

The socket concept was developed in the first networked version of UNIX developed at the University of California at Berkeley. So sockets are also known as Berkeley Sockets.

Q.34  What is Client-Server terminology?

It is a common term related to networking.

A server is a machine that has some resource that can be shared.

A server may also be a proxy server. Proxy server is a machine that acts as an intermediate between the client and the actual server.

It performs a task like authentications, filtering and buffering of data etc. it communicates with the server on behalf of the client.

A client is any machine that wants to use the services of a particular server.

The server is a permanently available resource, while the client is free to disconnect after its job is done.

Q.35  What is Spring?

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.

Q.36  What are features of Spring?

Lightweight:

Spring is lightweight in size and transparency. The basic version is around 1MB. And the processing overhead is also very less.

Inversion of control (IOC):

Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.

Aspect-oriented (AOP):

Spring supports Aspect-oriented programming and enables cohesive development by separating application business logic from system services.

Container:

Spring contains and manages the lifecycle and configuration of application objects.

MVC Framework:

Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.

JDBC Exception Handling:

The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy. Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO, and iBATIS.

Transaction Management:

Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring’s transaction support is not tied to J2EE environments and it can be also used in containerless environments.

Q.37  Which are the Spring framework modules?

  • 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

Q.38  Explain Spring configuration file?

It is an XML file, this file contains the classes information and describes how these classes are configured and introduced to each other.

Q.39  What is Dependency Injection?

Dependency Injection is a simple example of Inversion of Control. It is a design pattern to provide loose coupling. It removes the dependency from the program.

Q.40  What are the types of IOC container in spring?

There are two types of IOC containers in spring framework.

  1. BeanFactory
  2. ApplicationContext

Q.41  What are the different ways to inject dependency in Spring?

There are two ways to do dependency injection in Spring.

Using setter method

Using constructor.

Q.42  Which is the best way of injecting beans and why?

The best approach is to use constructor arguments for compulsory dependencies and setters for optional ones. Constructor injection method allows injecting values to immutable fields and makes testing easier.

Q.43  Name some of the Design Patterns used in the Spring Framework?

Singleton Pattern, Factory Pattern, Prototype Pattern, Adapter Pattern, Proxy Pattern, Template Method Pattern, Front Controller, Data Access Object, Model View Controller.

Q.44  What is the benefit of IOC?

  • It reduces the amount of code in your application.
  • Use of IOC makes your application easy to test
  • Loose coupling is promoted with minimum effort and less disturbance.
  • IOC containers support eager instantiation and lazy loading of services.

Q.45  What is the Differentiate between BeanFactory and ApplicationContext.

BeanFactory ApplicationContext
It is an interface defined in org.springframework.beans.factory.BeanFactory It is an interface defined in org.springframework.context.ApplicationContext
It uses Lazy initialization It uses Eager initialization
It explicitly provides a resource object using the syntax It creates and manages resource objects on its own
It will not support internationalization It will supports internationalization
It doesn’t support annotation based dependency It supports annotation based dependency

 Q.46  What is Hibernate?

Hibernate is an open-source and lightweight ORM (Object/Relational mapping) tool which is used to store, manipulate and retrieve data from the database without writing complex database queries.

Q.47  What is ORM?

ORM (Object Relational Mapping) is the fundamental concept of Hibernate framework which maps database tables with Java Objects and then provides various API’s using which we can perform different types of operations on the data tables.

Q.48  What are the advantages of Hibernet over JDBC?

JDBC does not have any ORM tool. Hibernate have ORM (Object-relational Mapping) tools which simplify all the works.

In JDBC developer is responsible to write the SQL statements for taking and closing the connection. In Hibernate HRS (Hibernate Runtime System) is responsible for taking the connections, creating the statement and releasing the connections.

we can set more than one Isolation Level in JDBC In hibernate only one isolation level.

JDBC doesn’t support a distributed transaction. Hibernate support distributed transaction with CME(Container Managed Environment)

Q.49  Name some databases that Hibernate supports.

  • MySQL
  • PostgreSQL
  • FrontBase
  • Oracle
  • Sybase SQL Server
  • DB2/NT
  • Microsoft SQL Server Database

Q.50  What is HQL (Hibernate Query Language)?

Hibernate Query Language is known as an object-oriented query language. It is like structured query language (SQL) except that we use Objects instead of table names. HQL is case-insensitive except for java class and variable names.

Conclusion:

Thus we have completed 101 Advance Java Interview Questions listing. These are extremely useful Advance Java Interview Questions for beginners and experienced. We are sure that these questions will help you understand Advance Java and crack your interview.

If you think we have missed anything important, please comment. Will try to accommodate it.

Add a Comment

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