Advanced Java Viva Questions
Java is one of the most demanding languages in the market today. As a fact, ten million developers across the globe work on the Java programming language, though the number is increasing day by day. So if you are a Java developer, a budding Java aspirant, or a computer science student, you will most likely be attending a Java interview or a Java viva exam. Most commonly, the interviewers ask Advance Java questions to check your programming knowledge.
Below given are the most popular advance Java viva 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 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 programmers 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:
Syntax for writing data from a Buffer:
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:
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 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 Java.io 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 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 occurs through Socket only enables to send packets of raw data between applications.
11) What do you understand by synchronization? How does it benefits in 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