Java interview questions

Java quiz questions

  • 1.

    Explain the life cycle of a Servlet.

    Answer:

    On every client’s request, the Servlet Engine loads the servlets and invokes its init methods, in order for the servlet to be initialized. Then, the Servlet object handles all subsequent requests coming from that client, by invoking the service method for each request separately. Finally, the servlet is removed by calling the server’s destroy method.

    View
  • 2.

    What is the difference between GenericServlet and HttpServlet ?

    Answer:

    GenericServlet is a generalized and protocol-independent servlet that implements the Servlet and ServletConfig interfaces. Those servlets extending the GenericServlet class shall override the service method. Finally, in order to develop an HTTP servlet for use on the Web that serves requests using the HTTP protocol, your servlet must extend the HttpServlet instead. Check Servlet examples here.

    View
  • 3.

    What is the difference between an Applet and a Servlet ?

    Answer:

    An Applet is a client side java program that runs within a Web browser on the client machine. On the other hand, a servlet is a server side component that runs on the web server.An applet can use the user interface classes, while a servlet does not have a user interface. Instead, a servlet waits for client’s HTTP requests and generates a response in every request.

    View
  • 4.

    Explain the architechure of a Servlet.

    Answer:

    The core abstraction that must be implemented by all servlets is the javax.servlet.Servlet interface. Each servlet must implement it either directly or indirectly, either by extending javax.servlet.GenericServlet or javax.servlet.http.HTTPServlet. Finally, each servlet is able to serve multiple requests in parallel using multithreading.

    View
  • 5.

    What is a Servlet ?

    Answer:

    The servlet is a Java programming language class used to process client requests and generate dynamic web content. Servlets are mostly used to process or store data submitted by an HTML form, provide dynamic content and manage state information that does not exist in the stateless HTTP protocol.

    View
  • 6.

    Explain Serialization and Deserialization.

    Answer:

    Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes and includes the object’s data, as well as information about the object’s type, and the types of data stored in the object. Thus, serialization can be seen as a way of flattening objects, in order to be stored on disk, and later, read back and reconstituted. Deserialisation is the reverse process of converting an object from its flattened state to a live object.

    View
  • 7.

    Explain Marshalling and demarshalling.

    Answer:

    When an application wants to pass its memory objects across a network to another host or persist it to storage, the in-memory representation must be converted to a suitable format. This process is called marshalling and the revert operation is called demarshalling.

    View
  • 8.

    What is the purpose of using RMISecurityManager in RMI ?

    Answer:

    RMISecurityManager provides a security manager that can be used by RMI applications, which use downloaded code. The class loader of RMI will not download any classes from remote locations, if the security manager has not been set.

    View
  • 9.

    What is DGC ? And how does it work ?

    Answer:

    DGC stands for Distributed Garbage Collection. Remote Method Invocation (RMI) uses DGC for automatic garbage collection. Since RMI involves remote object references across JVM’s, garbage collection can be quite difficult. DGC uses a reference counting algorithm to provide automatic memory management for remote objects.

    View
  • 10.

    What is the role of stub in RMI ?

    Answer:

    A stub for a remote object acts as a client’s local representative or proxy for the remote object. The caller invokes a method on the local stub, which is responsible for executing the method on the remote object. When a stub’s method is invoked, it undergoes the following steps:

    • It initiates a connection to the remote JVM containing the remote object.
    • It marshals the parameters to the remote JVM.
    • It waits for the result of the method invocation and execution.
    • It unmarshals the return value or an exception if the method has not been successfully executed.
    • It returns the value to the caller.
    View
  • 11.

    What are the steps involved to make work a RMI program ?

    Answer:

    The following steps must be involved in order for a RMI program to work properly:

    • Compilation of all source files.
    • Generatation of the stubs using rmic.
    • Start the rmiregistry.
    • Start the RMIServer.
    • Run the client program.
    View
  • 12.

    What is the difference between using bind() and rebind() methods of Naming Class ?

    Answer:

    The bind method bind is responsible for binding the specified name to a remote object, while the rebind method is responsible for rebinding the specified name to a new remote object. In case a binding exists for that name, the binding is replaced.

    View
  • 13.

    What is meant by binding in RMI ?

    Answer:

    Binding is the process of associating or registering a name for a remote object, which can be used at a later time, in order to look up that remote object. A remote object can be associated with a name using the bind or rebind methods of the Naming class.

    View
  • 14.

    What is the role of the java.rmi.Naming Class ?

    Answer:

    The java.rmi.Naming class provides methods for storing and obtaining references to remote objects in the remote object registry. Each method of the Naming class takes as one of its arguments a name that is a String in URL format.

    View
  • 15.

    What is the role of Remote Interface in RMI ?

    Answer:

    The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. A class that implements a remote interface should declare the remote interfaces being implemented, define the constructor for each remote object and provide an implementation for each remote method in all remote interfaces.

    View
  • 16.

    What are the layers of RMI Architecture ?

    Answer:

    The RMI architecture consists of the following layers:

    • Stub and Skeleton layer: This layer lies just beneath the view of the developer. This layer is responsible for intercepting method calls made by the client to the interface and redirect these calls to a remote RMI Service.
    • Remote Reference Layer: The second layer of the RMI architecture deals with the interpretation of references made from the client to the server’s remote objects. This layer interprets and manages references made from clients to the remote service objects. The connection is a one-to-one (unicast) link.
    • Transport layer: This layer is responsible for connecting the two JVM participating in the service. This layer is based on TCP/IP connections between machines in a network. It provides basic connectivity, as well as some firewall penetration strategies.
    View
  • 17.

    What is the basic principle of RMI architecture ?

    Answer:

    The RMI architecture is based on a very important principle which states that the definition of the behavior and the implementation of that behavior, are separate concepts. RMI allows the code that defines the behavior and the code that implements the behavior to remain separate and to run on separate JVMs.

    View
  • 18.

    What is RMI ?

    Answer:

    The Java Remote Method Invocation (Java RMI) is a Java API that performs the object-oriented equivalent of remote procedure calls (RPC), with support for direct transfer of serialized Java classes and distributed garbage collection. Remote Method Invocation (RMI) can also be seen as the process of activating a method on a remotely running object. RMI offers location transparency because a user feels that a method is executed on a locally running object. Check some RMI Tips here.

    View
  • 19.

    What does Connection pooling mean ?

    Answer:

    The interaction with a database can be costly, regarding the opening and closing of database connections. Especially, when the number of database clients increases, this cost is very high and a large number of resources is consumed.A pool of database connections is obtained at start up by the application server and is maintained in a pool. A request for a connection is served by a connection residing in the pool. In the end of the connection, the request is returned to the pool and can be used to satisfy future requests.

    View
  • 20.

    What is the use of CallableStatement ? Name the method, which is used to prepare a CallableStatement.

    Answer:

    CallableStatement is used to execute stored procedures. Stored procedures are stored and offered by a database. Stored procedures may take input values from the user and may return a result. The usage of stored procedures is highly encouraged, because it offers security and modularity.The method that prepares a CallableStatement is the following:

    CallableStament.prepareCall();

     

    View

© 2017 QuizBucket.org