Sr.Java Web Developer Interview Preparation Guide
Download PDF

Sr.Java Web Developer related Frequently Asked Questions by expert members with professional career as Sr.Java Web Developer. These list of interview questions and answers will help you strengthen your technical skills, prepare for the new job interview and quickly revise your concepts

64 Sr.Java Web Developer Questions and Answers:

Table of Contents

Sr.Java Web Developer Interview Questions and Answers
Sr.Java Web Developer Interview Questions and Answers

1 :: Do you know what is composition in Java?

Composition is again specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.

2 :: Please explain what is the difference between execute, executeQuery, executeUpdate?

Statement execute(String query) is used to execute any SQL query and it returns TRUE if the result is an ResultSet such as running Select queries. The output is FALSE when there is no ResultSet object such as running Insert or Update queries. We can use getResultSet() to get the ResultSet and getUpdateCount() method to retrieve the update count.

Statement executeQuery(String query) is used to execute Select queries and returns the ResultSet. ResultSet returned is never null even if there are no records matching the query. When executing select queries we should use executeQuery method so that if someone tries to execute insert/update statement it will throw java.sql.SQLException with message “executeQuery method can not be used for update”.

Statement executeUpdate(String query) is used to execute Insert/Update/Delete (DML) statements or DDL statements that returns nothing. The output is int and equals to the row count for SQL Data Manipulation Language (DML) statements. For DDL statements, the output is 0.

You should use execute() method only when you are not sure about the type of statement else use executeQuery or executeUpdate method.

3 :: Tell us what is Hibernate Framework?

Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is java based ORM tool that provides framework for mapping application domain objects to the relational database tables and vice versa.

Hibernate provides reference implementation of Java Persistence API, that makes it a great choice as ORM tool with benefits of loose coupling. We can use Hibernate persistence API for CRUD operations. Hibernate framework provide option to map plain old java objects to traditional database tables with the use of JPA annotations as well as XML based configuration.

Similarly hibernate configurations are flexible and can be done from XML configuration file as well as programmatically.

4 :: Tell me what are the different tags provided in JSTL?

There are 5 type of JSTL tags.

☛ core tags
☛ sql tags
☛ xml tags
☛ internationalization tags
☛ functions tags

5 :: Tell us what is difference between Error and Exception?

An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors you can not repair them at runtime.Though error can be caught in catch block but the execution of application will come to a halt and is not recoverable.

While exceptions are conditions that occur because of bad input or human error etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.

6 :: Do you know how to create a custom Exception?

To create you own exception extend the Exception class or any of its subclasses.

☛ class New1Exception extends Exception { } // this will create Checked Exception
☛ class NewException extends IOException { } // this will create Checked exception
☛ class NewException extends NullPonterExcpetion { } // this will create UnChecked exception

7 :: Please explain can we write multiple catch blocks under single try block?

Yes we can have multiple catch blocks under single try block but the approach should be from specific to general. Let’s understand this with a programmatic example.

public class Example {
public static void main(String args[]) {
try {
int a[]= new int[10];
a[10]= 10/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic exception in first catch block");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out of bounds in second catch block");
}
catch(Exception e)
{
System.out.println("Any exception in third catch block");
}
}

8 :: What is String toString()?

This method returns the information in String format. The returned String contains the name of Throwable class and localized message.

9 :: Tell me when will you use Servlet and JSP or MVC framework?

While framework provides a number of components and allows one to concentrate more on the business logic but Servlets and JSP are used for controller and view layer respectively.

10 :: Explain me what is a servlet?

☛ Java Servlet is server side technologies to extend the capability of web servers by providing support for dynamic response and data persistence.
☛ The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing our own servlets.
☛ All servlets must implement the javax.servlet.Servlet interface, which defines servlet lifecycle methods. When implementing a generic service, we can extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.
☛ Most of the times, web applications are accessed using HTTP protocol and thats why we mostly extend HttpServlet class. Servlet API hierarchy is shown in below image.

11 :: Tell me what is JDBC Driver?

JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers:

☛ JDBC-ODBC bridge driver
☛ Native-API driver (partially java driver)
☛ Network Protocol driver (fully java driver)
☛ Thin driver (fully java driver)

12 :: What is execute(String query)?

Statement execute(String query) is used to execute any SQL query and it returns TRUE if the result is an ResultSet such as running Select queries. The output is FALSE when there is no ResultSet object such as running Insert or Update queries. We can use getResultSet() to get the ResultSet and getUpdateCount() method to retrieve the update count.

13 :: Can you list some of the important annotations in annotation-based Spring configuration?

The important annotations are:

☛ @Required
☛ @Autowired
☛ @Qualifier
☛ @Resource
☛ @PostConstruct
☛ @PreDestroy

14 :: Explain me what are the advantages of Hibernate over JDBC?

Some of the important advantages of Hibernate framework over JDBC are:

☛ Hibernate removes a lot of boiler-plate code that comes with JDBC API, the code looks more cleaner and readable.
☛ Hibernate supports inheritance, associations and collections. These features are not present with JDBC API.
☛ Hibernate implicitly provides transaction management, in fact most of the queries can’t be executed outside transaction. In JDBC API, we need to write code for transaction management using commit and rollback.
☛ JDBC API throws SQLException that is a checked exception, so we need to write a lot of try-catch block code. Most of the times it’s redundant in every JDBC call and used for transaction management. Hibernate wraps JDBC exceptions and throw JDBCException or HibernateException un-checked exception, so we don’t need to write code to handle it. Hibernate built-in transaction management removes the usage of try-catch blocks.
☛ Hibernate Query Language (HQL) is more object oriented and close to java programming language. For JDBC, we need to write native sql queries.
☛ Hibernate supports caching that is better for performance, JDBC queries are not cached hence performance is low.
☛ Hibernate provide option through which we can create database tables too, for JDBC tables must exist in the database.
☛ Hibernate configuration helps us in using JDBC like connection as well as JNDI DataSource for connection pool. This is very important feature in enterprise application and completely missing in JDBC API.

► Hibernate supports JPA annotations, so code is independent of implementation and easily replaceable with other ORM tools. JDBC code is very tightly coupled with the application.

15 :: Do you know what is the jspDestroy() method?

jspDestry() method is invoked from javax.servlet.jsp.JspPage interface whenever a JSP page is about to be destroyed. Servlets destroy methods can be easily overridden to perform cleanup, like when closing a database connection.

16 :: Please explain what is exception hierarchy in java?

The hierarchy is as follows:

Throwable is a parent class of all Exception classes. There are two types of Exceptions: Checked exceptions and UncheckedExceptions or RunTimeExceptions. Both type of exceptions extends Exception class whereas errors are further classified into Virtual Machine error and Assertion error.

17 :: Explain me what are the important methods of Java Exception Class?

Methods are defined in the base class Throwable. Some of the important methods of Java exception class are stated below.

☛ String getMessage() – This method returns the message String about the exception . The message can be provided through its constructor.
☛ public StackTraceElement[] getStackTrace() – This method returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack whereas the last element in the array represents the method at the bottom of the call stack.
☛ Synchronized Throwable getCause() – This method returns the cause of the exception or null id as represented by a Throwable object.
☛ String toString() – This method returns the information in String format. The returned String contains the name of Throwable class and localized message.
☛ void printStackTrace() – This method prints the stack trace information to the standard error stream.

18 :: Explain me what are the common browser issues you will keep in mind while creating a web application?

☛ 1) User pressing back/refresh button
☛ 2) Browser crashing
☛ 3) Session issues
☛ 4) Compatibility across web browsers

20 :: Explain me what is the life-cycle of a servlet?

There are 5 stages in the lifecycle of a servlet:

☛ Servlet is loaded
☛ Servlet is instantiated
☛ Servlet is initialized
☛ Service the request
☛ Servlet is destroyed

21 :: Tell me what are the JDBC API components?

The java.sql package contains interfaces and classes for JDBC API.

Interfaces:
☛ Connection
☛ Statement
☛ PreparedStatement
☛ ResultSet
☛ ResultSetMetaData
☛ DatabaseMetaData
☛ CallableStatement etc.

Classes:
☛ DriverManager
☛ Blob
☛ Clob
☛ Types
☛ SQLException etc.

22 :: What is executeUpdate(String query)?

Statement executeUpdate(String query) is used to execute Insert/Update/Delete (DML) statements or DDL statements that returns nothing. The output is int and equals to the row count for SQL Data Manipulation Language (DML) statements. For DDL statements, the output is 0.

23 :: Explain me how to handle exceptions in Spring MVC Framework?

Spring MVC Framework provides the following ways to help us achieving robust exception handling.

☛ Controller Based:
We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation.

☛ Global Exception Handler:
Exception Handling is a cross-cutting concern and Spring provides @ControllerAdvice annotation that we can use with any class to define our global exception handler.

☛ HandlerExceptionResolver implementation:
For generic exceptions, most of the times we serve static pages. Spring Framework provides HandlerExceptionResolver interface that we can implement to create global exception handler. The reason behind this additional way to define global exception handler is that Spring framework also provides default implementation classes that we can define in our spring bean configuration file to get spring framework exception handling benefits.

24 :: Explain me what are the important benefits of using Hibernate Framework?

Some of the important benefits of using hibernate framework are:

☛ Hibernate eliminates all the boiler-plate code that comes with JDBC and takes care of managing resources, so we can focus on business logic.
☛ Hibernate framework provides support for XML as well as JPA annotations, that makes our code implementation independent.
☛ Hibernate provides a powerful query language (HQL) that is similar to SQL. However, HQL is fully object-oriented and understands concepts like inheritance, polymorphism and association.
☛ Hibernate is an open source project from Red Hat Community and used worldwide. This makes it a better choice than others because learning curve is small and there are tons of online documentations and help is easily available in forums.
☛ Hibernate is easy to integrate with other Java EE frameworks, it’s so popular that Spring Framework provides built-in support for integrating hibernate with Spring applications.
☛ Hibernate supports lazy initialization using proxy objects and perform actual database queries only when it’s required.
☛ Hibernate cache helps us in getting better performance.
☛ For database vendor specific feature, hibernate is suitable because we can also execute native sql queries.

Overall hibernate is the best choice in current market for ORM tool, it contains all the features that you will ever need in an ORM tool.

25 :: Please explain what purpose does the keywords final, finally, and finalize fulfill?

Final:
Final is used to apply restrictions on class, method and variable. Final class can’t be inherited, final method can’t be overridden and final variable value can’t be changed. Let’s take a look at the example below to understand it better.

class FinalVarExample {
public static void main( String args[])
{
final int a=10; // Final variable
a=50; //Error as value can't be changed
}
Finally
Finally is used to place important code, it will be executed whether exception is handled or not. Let’s take a look at the example below to understand it better.

class FinallyExample {
public static void main(String args[]){
try {
int x=100;
}
catch(Exception e) {
System.out.println(e);
}
finally {
System.out.println("finally block is executing");}
}}
}
Finalize
Finalize is used to perform clean up processing just before object is garbage collected. Let’s take a look at the example below to understand it better.

class FinalizeExample {
public void finalize() {
System.out.println("Finalize is called");
}
public static void main(String args[])
{
FinalizeExample f1=new FinalizeExample();
FinalizeExample f2=new FinalizeExample();
f1= NULL;
f2=NULL;
System.gc();
}
}