Java Interview Questions

Java Interview Questions

Crack Your Interview

Advanced Java Interview Questions

Advanced Java Interview Questions

1) What is the use of garbage collection in Java?

The use of garbage collection in Java is to recognize and remove objects that are no more required by a program and are unnecessary consuming the program resources that could be freed and reused by other objects. An object in Java is subject to garbage collection when it turns inaccessible to the Java program in which it is initiated.

2) What are Lambda Expressions?

Lambda Expressions, often called lambdas, are a new feature introduced with Java 8. They provide an easier way to work with interfaces that have only got one method, and they are often used in places where the programmer use anonymous classes.

Sometimes we need to write several lines to accomplish what we want to do when all we care about is to print any statement. Using a Lambda expression, we can do that in a single line. Therefore, increasing the program efficiency by reducing the lines of codes.

3) What do you understand by a thread in Java?

A thread is a single sequential flow of control within a process where each process can contain two or more “threads”. In Java, a thread is created and coordinated by the java.lang.Thread class.

Every process (or application) in Java language contains at least one thread (also known as the main thread). Depending on the programmer’s need, the process can contain multiple threads as well to handle different tasks such as memory management and 1/0.

4) What is the syntax to read and write data from a Buffer?

Syntax for reading data from a Buffer:

  1. int datobject =;   

Syntax for writing data from a Buffer:

  1. int datobject = inChannel.write(buff);  

5) Why would we want to use multiple threads in our application? Why not just stick with the main thread?

There are two main reasons for this:

  • First of all, we sometimes want to perform a task that’s going to take a long time. For example, we might want to query a database, or we might want to fetch data from somewhere on the Internet. We could do this on the main thread, but the code within each main thread executes in a linear fashion. The main thread won’t be able to do anything else while it’s waiting for the data.
  • The second way of putting this is that the execution of the main thread will be suspended. It has to wait for the data to be returned before it can execute the next line of code. To the user, this could appear as if our application has died or is frozen, especially when we’re dealing with a UI application.

Therefore, instead of tying up the main thread, we can create multiple threads and execute the long-running task on those threads. This would free up the main thread, so that it can continue executing. This process is called Multithreading. It can report progress or accept user input while the long-running task continues to execute in the background.

6) When and how a deadlock occurs in any program?

A deadlock occurs when two or more threads are blocked on locks and every thread that’s blocked is holding a lock that another block thread wants. For example thread 1 is holding lock 1 and waiting to acquire lock 2 but thread 2 is holding lock 2 and waiting to acquire lock 1. It creates a situation where all the threads holding the locks are blocked therefore it will never release the locks they’re holding and so none of the waiting threads will actually ever run.

7) Why do developers use BufferedReader to read any file in Java?

A BufferedReader reads text from the input stream and buffers the characters into a character array. Reading chunks of data from a stream (unlike a file) is more efficient than reading just a few characters at a time.

Therefore we can conclude that BufferReader is more streamlined and faster. Also, we can specify the size of the buffer, but the default is 8k, and that is also sufficient for most purposes.

8) Define Java Nio.

In Java 1.4, a new I/O package was added to the Java SDK called Java.nio The package was introduced as an expansion to Java I/O because the classes in the package implemented 1/0 in a non-blocking manner. The package Java.nio was also meant to fix some of the problems developers could run into when using the classes to work with the file system.

9) Define JDBC Drivers?

The driver is simply a Java library containing classes that implement the JDBC API. Because all JDBC drivers have to implement the same interfaces, it’s not difficult to change the data source an application uses. For example, if an application uses an SQLite database, and then we decide later that we want to use a MySQL database, all we have to do is use the MySQL JDBC driver instead of the SQLite one (in addition to migrating the data to a MySQL DB, of course).

JDBC consists of two packages: Java.sql (core JDBC) and Javax.sql (optional JDBC). The APIs in the Javax.sql package is required when working with database servers. We’ll talk about those later. All the popular databases provide JDBC drivers. The JDK ships with a database called a derby, which can be used for desktop applications or when prototyping. The derby JDBC driver is also included in the JDK.

10) Mention the advantages and disadvantages of using Java Sockets.

Advantages of Java Sockets: Sockets are used in Java programming because of their flexibility and easy communication protocols. They also cause low network traffic such as HTML forms and CGI scripts that create and transfer the complete web pages for each new data request.

Disadvantages of Java Sockets: The communications that occur through Socket only enable to sending of packets of raw data between applications.

11) What do you understand by synchronization? How does it benefit multithreading?

With respect to multithreading, synchronization is the process of controlling the access of multiple threads to shared resources. In Java, we can synchronize methods and statements. Whenever we synchronize any method in Java program, it allows only one thread to execute at one time. Therefore, if a thread is using a synchronize method, all the other threads that want to execute the same method or any other synchronized method initiated in the same class will suspend until the execution of the running thread is completed.

12) Explain the use of the JSTL core tag with the help of an example.

The JSTL <c: for each > is an iteration tag used to loop or repeat the nested body content a fixed number of times. This tag is an alternative for Java loops such as while, do-while, or for.


  1. <%@ taglib uri=”” prefix=”c” %>    
  2. <html>    
  3. <head>    
  4. <title> forEach Core Tag </title>    
  5. </head>    
  6. <body>    
  7. <c:forEach var=”j” begin=”1″ end=”4″>    
  8.    Item <c:out value=”${j}”/><p>    
  9. </c:forEach>    
  10. </body>    
  11. </html>    


Advanced Java Viva Questions

13) What do you understand by JavaMail?

The JavaMail is an API widely used in Java programming to facilitate mailing operations such as composing, reading, sending, and receiving electronic mails on the Internet.

It is a protocol-independent and platform-independent framework available as an optional package with the Java SE platform and incorporated in the Java EE platform as well.

The JavaMail API can be used for many conducting other events. It can be used to register the user (sending notifications unlike thank you for visiting my website or subscribing to our newsletter), forgot the password (set and update a new password through mail), sending update notifications, etc,

14) Name the two core packages used in JavaMail API.

The two packages that contain the core classes of JavaMail API are given below:

  1. mail
  2. mail.activation

15) Name the JSTL tags.

JSTL includes five types of tags:

  • Core Tags
  • Function Tags
  • Formatting Tags
  • XML Tags
  • SQL Tags

16) In Java, who creates the object of Servlets?

Web container or servlet container is responsible to create the object of Servlets.

17) What is the role of RequestDispatcher Interface in Servlet?

The RequestDispacher interface in Servlet is used to dispatch requests to other resources such as HTML, Servlet or JSP. However, it can also be used to incorporate the content of another resource.

18) What is the role of attributes in servlets?

An attribute is a map object that enables the Servlets to share information among themselves (mainly between one servlet to another). Attributes can be used to set, get or remove in request, session or application scope.

19) What is Session Tracking? What are the common methods of Session Tracking?

In JSP, a Session means a specific interval of time used for tracking and maintaining user data for a particular time frame.

Session Tracking is a mechanism used by the web container to maintain the state of a user. The HTTP and web servers are stateless protocols. Therefore all requests and responses are independent. Every time a user requests to the server, it is treated as a new request. So to prevent this and to maintain the state of a user (recognize to a particular user), we use Session Tracking.

The common methods used in Session Tracking are given below:

  • Cookies
  • HttpSession
  • Hidden Form Field
  • URL Rewriting

20) Does JSP allow the use of exception implicit objects on any page?

The exception implicit object was created only for the JSP error page and is defined using the isErrorPage attribute. Therefore, we cannot use the exception implicit object in any JSP page.

21) Can we forward the request from a JSP page to a servlet page?

Yes, we can forward a request from the JSP page to Servlet by using the JSP “forward action” tag and by mentioning the URL pattern of the Servlet.

22) Is JSP an extensible technology?

JSP technology is extensible through the development of tags or custom actions that are encapsulated in tag libraries.

23) Define Static Initializer.

In Java, the static initializer is defined as a static {} block of code that runs only once before the constructor or main method is called. It is useful when the user wants to execute a complex calculation to determine the ‘n’ value or if its value is fetched from a database.


  1. class StaticInitializerExample {  
  2.     public static int n;  
  3.     static {  
  4.         n = 12;  
  5.     }  
  6.     // usually the constructors or the main method is called here?  
  7. }  

24) What is the use of Java Transaction API (JTA)?

The Java Transaction API (JTA) permits the applications to execute distributed transactions that can access and update data on multiple networked computing resources.

25) How is a transaction different from distributed transaction?

A transaction is defined as a logical unit of work that either succeeds or generates no result. In contrast, a Distributed transaction can access and update data on multiple computing networked resources.

26) Mention the different transactional attributes?

The different transactional attributes are as follows:

  1. Required,
  2. RequiresNew,
  3. Mandatory,
  4. Supports,
  5. NotSupported,
  6. Never