Advanced Java is an extension of Core and Basic Java and is frequently asked in interviews to check the base knowledge and the level of training of an individual. They are especially asked by the HR recruiters in IT companies to recruit the fresh talent for their organisation.
Given below are top 30 advanced Java questions-answer sets frequently asked in interviews:
1. Define synchronization. Why is synchronization important?
Synchronization is the ability to control the access to shared resources with respect to multiple threads. It is considered to be important as without synchronization, it is possible only for one thread to modify a shared object if another thread is using or updating that object’s value i.e. only one thread gets access to the resources at any point of time. Synchronization removes this possibility, thereby eliminating significant errors.
2. Differentiate between pre-emptive scheduling and time slicing.
Pre-emptive scheduling allows the execution of the highest priority task until it enters the waiting or dead states or a task with even higher priority comes into existence. On the other hand, time slicing permits the execution of a task for a predefined slice of time and later re-enters the pool of ready tasks. The task to be executed next is determined by the scheduler which decides based on priority and other factors.
3. Describe the purpose of the wait(), notify(), and notifyAll() methods.
All the three methods: the wait() method, the notify() method, and the notifyAll() method, provide an effective way for threads to communicate with each other. A thread which executs an object’s wait() method will enter the waiting state and only enters the ready state after another thread summons the object’s notify() or notifyAll() methods. notify() method notifies only one thread that is waiting for the lock on that object. notifyAll() notifies all the threads waiting for a lock on a particular object.
4. What are Java source code files named as?
A Java source code file is given the name of a public class or interface, defined within the file, which may hold at most one public class or interface. The source code file must take the name of the public class or interface, if it is defined within a source code file. Also, the file must take on a name that is different than its classes and interfaces, when no public class or interface is defined within a source code file. The .java extension is used by source code files.
5. What is an object’s lock and which objects have locks?
The mechanism used by multiple threads to obtain synchronized access to the object is known as an Object’s Lock. It is only after acquiring the object’s lock that a thread can execute a synchronized method of an object. Locks are provided to all objects and classes.
6. Describe the organization of the elements of a GridBagLayout.
The organization of the elements of a GridBagLayout is in the grid format. But, the sizes of the elements are different and so, they may occupy more than one row or column of the grid. Also, the sizes of the rows and columns may also vary.
7. What advantage do Java’s layout managers provide over traditional windowing systems?
The advantage Java’s layout managers have over traditional windowing systems is that Java’s layout managers aren’t tied to total sizing and positioning as they lay out components in a steady manner across all windowing platforms. This allows them to accommodate platform-specific variances among windowing systems.
8. Differentiate between exception and error.
The exception class defines mild error conditions that the program encounters. Eg: FilenotFound, Arithmetic exception can occur when you try to open a file that does not exist, the network connection is disrupted, operators being used are out of the prescribed range.
The error class defines serious error conditions that you should not attempt to recover from. In case an error is encountered, it is advisable to let the program terminate. Eg: Running out of memory error, stack overflow error.
9. Differentiate between static and non-static variables.
A variable associated with the class as a whole rather than with specific instances of a class is known as a static variable, while a variable with unique values with each object instance is known as a non-static variable. Memory for static variable is allocated at the time of loading the class where as memory for non-static variables is allocated when an object is created.
//Example of static and non-static variable
int a; //non-static variable
static int b; //static variable
10. What is an applet? Should applets have constructors?
Applets are small programs transferred through the Internet and run on the web browser. Applets implement functionality of a client.
Applet is an interactive program that runs inside a web page displayed by a java capable browser. There is no concept of a constructor in Applets. Applets are invoked either through browser or through Appletviewer utility provided by JDK.
11. Describe the process of multithreading on a computer with a single CPU.
On a computer with a single CPU, multithreading takes place when the operating system’s task scheduler allots limited execution time to multiple tasks. Those multiple tasks are executed by quickly switching between them, which creates the illusion that the tasks have been executed sequentially.
12. Explain Casting.
Casting is of two types: casting between primitive numeric types and casting between object references. To convert larger values, to smaller values, casting between numeric types is used. On the other hand, to refer to an object by a compatible class, interface, or array type reference, Casting between object references is used.
13. What is the difference between a Choice and a List?
Display in a compact form requiring you to pull it down to see the list of available choices is known as a Choice. In Choice, only one option can be selected. Display of several visible List items is defined as a List. The selection of one or more items is possible and allowed in a list.
14. Does garbage collection guarantee that a program will not run out of memory?
Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for the programs to create objects that are not subject to garbage collection.
15. How is it possible for two String objects with identical values not to be equal under the == operator?
It possible for two String objects with identical values not to be equal under the == operator as this operator compares two objects to determine if they are the same object in memory. If they are, then they are located indifferent areas of memory.
16. Define an I/O filter.
An object that takes input from one stream and gives output to another, altering the data in some way or the other, while it is passed from one stream to another is defined as an I/O filter.
17. What is the Set interface?
An interface that provides methods for retrieving the elements of a finite mathematical set and does not allow duplicate elements is called The Set Interface.
18. What are constructors in Java?
Constructors are defined as blocks of code used to alter an object, which is having the same name as that of the class. Having no return type, it is automatically summoned when an object is created. The two types of constructors are: Default constructor and Parameterized constructor.
//Example of a constructor
public student(int slno, String name)
19. What is singleton class and how can we make it so?
A class in which only one instance can be created at any given time, in one JVM, is called a Singleton Class. A Singleton Class can be made so by making its constructor private.
20. What are the differences between Heap and Stack Memory?
Stack memory is used only by one thread of execution and can only be accessed by that one thread, that is, it can’t be accessed by other threads. It Follows LIFO manner to free memory and manage it. It exists until the conclusion of execution of the thread.
Heap memory is used by all the parts of the application and the objects stored in the heap accessible all over. Its memory management is founded on generation associated to individual objects. Heap memory lives throughout, that is, from the start till the end of application execution.
21. What is association?
A relationship where all objects have their own lifecycle and there is no owner Is described as association. These relationships can be one to one, One to many, many to one and many to many.
22. What is a servlet?
Server-side technologies used to extend the abilities of web servers by giving support for active response and data persistence is a Servlet. Interfaces and classes for writing our own servlets is provided by the javax.servlet and javax.servlet.http packages.
23. What is Request Dispatcher?
Forwarding of the request to another resource like HTML, JSP or another servlet in the same application is done by RequestDispatcher interface. It can also be used to include the content of another resource to the response. There are two methods defined in this interface: void forward() and void include().
24. What is the life-cycle of a servlet?
There are 5 stages in the lifecycle of a servlet:
- Loading: The servlet is loaded
- Instantiation: The servlet is Instantiated
- Initialization: The servlet is Initialized
- Request: Service is requested
- Destruction: Servlet is destroyed
25. How do cookies work in Servlets?
Cookies are textual data sent by a server to the client which gets saved at the client’s local machine. Their method of working is:
- Servlet API’s provide cookies’ support through javax.servlet.http.Cookie class that implements Serializable and Cloneable interfaces.
- Since there is no point of adding Cookie to any request, there are no methods to set or add cookie to request.
- HttpServletRequest getCookies() method is provided to get the array of Cookies from request, while, HttpServletResponse addCookie(Cookie c) method is used to attach cookie in response header.
26. Expalin the different methods of session management in servlets?
A conversational state between a client and the server involving multiple requests and responses between them is called a Session. The only way to maintain a session, when HTTP and Web Server both are stateless, is to pass some unique information about the session (session id) between server and client in every request and response.
The most common ways of session management in servlets include: User Authentication, HTML Hidden Field, Cookies, URL Rewriting as well as Session Management API.
27. Define JDBC Driver and explain its types and its components.
JDBC Driver is a software module used to ensure and enable java application to interact and coordinate 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) and Thin driver (fully java driver).
JDBC API has the java.sql package which contains interfaces and classes for it. Interface includes Connection, Statement, PreparedStatement, ResultSet, ResultSetMetaData, DatabaseMetaData, CallableStatement and many more. Classes comprises of DriverManager, Blob, Clob, Types, SQLException etc.
28. What is JDBC Connection interface?
The JDBC Connection interface connects with and maintains a session with the database. It offers basic methods that returns the occurrence of Statement, PreparedStatement, CallableStatement and DatabaseMetaData. The interface includes: CreateStatement(), CreateStatement(resultSetType,resultSetCurrency), setAutoCommit(boolean status), Commit(), Rollback() and Close().
29. Describe the role of JDBC DriverManager class.
The role of JDBC DriverManager class is to register and unregister drivers and to manage the registered drivers. It offers a factory method to return the instance of the JDBC Connection interface.
30. Explain batch processing in JDBC?
Batch processing in JDBC allows the combination of group related SQL statements into a batch and its combined execution instead of executing a single query. Use of this technique in JDBC allows execution of multiple queries which makes performance faster.