Advanced Java Interview Questions and Answers
Java is used by 10 million global software developers as it is the most prominent programming language in the world. The learning of Java is good for the learners who are fresh graduates and final year students to kick-start their careers in software development. Here in this blog, we have framed popular Java Interview Questions and Answers for the aspirants to face the interviews successfully.
1. What is garbage collection in Java?
Garbage collection in Java is used to recognize and delete objects that are not needed by a program and are consuming the resources unnecessarily. The garbage collection process is applied to free up the space and reuse other objects. Java objects are subject to garbage collection when they are inaccessible to the Java program where they are initiated.
2. Define Lambda Expressions
Lambda Expressions are a new feature in Java 8 and they provide a simpler way to work with interfaces that have only a method used in places where the programmer uses anonymous classes. If the programmer is required to write some lines to complete what he wants to do with the use of a print statement, then he can write them in a single line using the lambda expression. The usage of lambda expressions increases the program’s efficiency by reducing the lines of code.
3. What is a thread in Java?
The thread is a single continuous flow of control within the process, where every process can consist of two or more “threads”. It is created and maintained by the java.lang.Thread class. Each process or application in Java programming language includes at least one thread that depends on the requirements of the programmer. The process includes multiple threads to manage various tasks like memory management and I/O
4. What is the syntax used to read and write data from the buffer?
Syntax for reading data from a Buffer
int dataobject = inChannel.read(buff);
Syntax for writing data from a Buffer
int dataobject = inChannel.write(buff);
5. Why should we use multiple threads in the application and why not with the main thread?
There are two main reasons to explain
- If we want to perform a task that takes a long time, it might be queried to the database or it should be fetched from the internet. It can be done easily with the main thread but the code within every main thread executes linearly. The main thread will not be able to perform anything else as it is waiting for the data.
- When the execution of the main thread is suspended, it has to wait for the data to be returned before it will execute the next line of code. This will appear as if our application has died or is frozen to the user.
We can create multiple threads to execute the long-running task on those threads, which helps free up the space using the main thread. It is continually executed and this process is called multithreading. It can report progress or accept user input during the long-running progress that continues to execute behind.
Get the fundamentals of programming with Java through our core Java training in Chennai.
6. Explain deadlock with its work on the program
The deadlock occurs when two or more threads are blocked on the lock that is holding a lock as per the thread’s wants. If thread 1 is holding lock 1 and waiting to obtain lock 2 but thread 2 is holding lock 2 and waiting to obtain lock 1, it creates a situation where all threads holding the locks are blocked for it will never release the locks holding, and none of the waiting threads will ever run.
7. Define BufferedReader
BufferReader is used to read text from the input stream and buffers the characters into a character array. Reading chunks of data from a stream is more effective than reading characters at a time. BufferReader is more efficient and faster for streamlining the size of data.
8. What is Java Nio?
A new I/O package is included in the Java SDK in Java 1.4 and it is known as Java.Nio. It is the expansion to Java I/O as the classes in the package are implemented inputs and outputs in a non-blocking way. The package Java.Nio is used to fix problems that developers could execute using Java.io classes to work with the file system.
9. Explain JDBC drivers
JDBC API will be implemented in the driver using a java library that contains classes. As all JDBC drivers implement the same interfaces, it’s not difficult to change the data source for application users. For instance, if an application uses an SQLite database, then the developer should decide that he wants to use a MySQL database, and all we have to do is use the MySQL JDBC driver for SQLite one. JDBC contains two packages such as Java.sql as core JDBC and Java.sql for optional JDBC. The APIs in the Java.sql package is needed when working with database servers.
10. What are the advantages and disadvantages of Java Sockets?
Advantages of Java Sockets: Sockets in Java programming are used for their flexibility and easy communication protocols. It leads to low network traffic like HTML forms and CGI scripts that generate and transfer the complete web pages for each new data request.
Limitation of Java Sockets: The communication that occurs through sockets enables to sending of packages of raw data between applications.
Our Java training program covers everything you need to know about application development.
11. What is synchronization and how does it benefit multithreading?
Synchronization is the process of managing the access of multiple threads to shared resources concerning multithreading. We can synchronize methods and statements in Java and whenever we synchronize methods in a Java program.
It enables only one thread to execute at one time. If a thread is using a synchronized method, all the other threads that want to run the same method or any other synchronized method initiated in the same class will suspend until the execution of the executing thread is completed successfully
12. Explain the use of the JSTL core tag with an example
The JSTL <c: for each > is an iteration tag used to loop or repeat the nested body content for a particular number of times. It is an alternative for Java loops such as while, do-while, or for.
Example
<%@ taglib url = http://java.sun.com/jsp/jstl/core prefix = “c” %>
<html> <head>
<title> forEach Core Tag </title>
</head>
<body>
<c:forEach var = “j” begin = “1” end = “4”>
Item <c:out value = “${j}” /><p>
</c:forEach>
</body>
</html>
Output:
Item 1
Item 2
Item 3
Item 4
13. What is JavaMail?
The JavaMail is a widely used API (Application Programming Interface) for facilitating mailing operations such as composing, reading, sending, and receiving electronic mails on the internet. It is a protocol-independent framework available with Java SE (Standard Edition) and associated with the Java EE (Enterprise Edition) platform. The JavaMail API will be used for various events and it can be used to register the user for sending notifications, set and update the new password for forgetting the password, and send update notifications.
14. What are the two packages used in JavaMail?
In JavaMail, there are two packages used for they contain various classes. The first one is mail and the second one is mail.activation.
15. What are the JSTL tags used in the Java programming language?
JSTL contains five types of tags and they are Core Tags, Function Tags, Formatting Tags, XML Tags, and SQL Tags.
16. Who is responsible for creating the objects of Servlets in Java?
Web container or servlet container is taking care of creating the object of servlets.
17. Define RequestDispatcher Interface in Servlet
The RequestDispatcher interface in Servlet is used to dispatch requests to other resources like HTML, JSP, or Servlet. It will also be used to incorporate the content of another resource.
18. What is the work of attribute in servlets?
An attribute is a map object that enables the Servlets to share information among themselves between one servlet to another and the attributes will be used to set, get, remove in request, application scope, or session.
Advance your development skills with our J2EE training in Chennai.
19. Define session tracking and explain the common methods of session tracking
Session means a specific interval time used to track and maintain user data for a particular time frame. Session tracking is a mechanism used by the web container to manage the state of a user. The HTTP and web servers are stateless protocols. All requests and responses are unique and every time a user sends requests to the server, it will be treated as a new request. By that time, we can use session tracking to prevent and maintain the state of a user. Following are the common methods used in session tracking
- Cookies
- HttpSession
- Hidden Form Field
- URL Rewriting
20. Can JSP allow the user of exception implicit objects in pages?
The exception implicit object will be created for the JSP error page and it is defined using the ErrorPage attribute. It can’t be used in the exception implicit object on any JSP page.
21. Can a request be forwarded from a JSP page to a servlet page?
Yes, a request can be forwarded from the JSP page to Servlet 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 by the development of tags or custom actions that are encapsulated in tag libraries.
23. Explain Static Initializer
The static initializer will be defined as a static {} block of code that executes only once before the constructor or main method is called for action. It is used when the user wants to run a complicated calculation to define an ‘n’ value or if its value is fetched from a database.
Example:
StaticInitializerExample.Java
class StaticInitializerExample
public static int n;
static
{
n = 12;
}
}
24. Explain the use of Java Transaction API
The JTA (Java Transaction API) allows the applications to execute distributed transactions that can access and update data on multiple networked computing resources
25. What is the difference between transaction and distributed transaction?
A transaction is defined as a logical unit of work that either succeeds or creates no result while the distributed transaction can access and update data on multiple computing networked resources.
Conclusion
Advanced Java is the most important skill as it contains Java Enterprise Edition, Servlet Programming, Web Services, The Persistence API, etc that are used for web and enterprise application development that are following client-server architecture. Learn the best Advanced Java Course in Chennai at Softlogic Systems to gain expertise in application development for web and enterprise.