May 10, 2020
Estimated Post Reading Time ~

Java Interview Question

Q. What is the difference between an Abstract class and Interface?
  • A. The main difference is methods of a Java interface are implicitly abstract and cannot have implementations.
  • A Java abstract class can have instance methods that implement a default behavior.
  • Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
  • Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
  • Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using the keyword “extends”.
  • An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
  • A Java class can implement multiple interfaces but it can extend only one abstract class.
  • The interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated but can be invoked if a main() exists.
  • In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
Q. What is JFC?
A. The Java Foundation Classes (JFC) are a comprehensive set of GUI components and services which dramatically simplify the development and deployment of commercial-quality desktop and Internet/Intranet applications.

Q. Explain 2 tier and 3 tier Architecture?
A. JDBC APIs support both the architectures - 2-tier and 3-tier models for accessing a relational database.

JDBC for a 2-tier architecture
In such an architecture, the Java application (or Applet) communicates directly with the data source (or database). The database may reside on the same machine or maybe on another machine to which the client machine needs to be connected through a network. In the latter case, it will be a client-server configuration and the server will hold the database. The client will send the statements to the database residing on the server and the result will be communicated back to the client.

In this architecture, the client machine will typically be a Java application and the server machine will have a Relational Database installed on it.

JDBC for a 3-tier architecture
In such an architecture, the client machine will send the database access statements to the middleware, which will then send the statements to the database residing on the third tier of the architecture, which is normally referred to as the back end. The statements will be processed there and the result is returned back to the client through the middle tier. This approach will have all the advantages associated with 3-tier architecture, such as better maintainability, easier deployment, scalability, etc.

This scenario will typically have a Java Applet or a Web Browser as the client tier, an Application Server as the Middle-tier, and a Relational DBMS as the Back-end.

Q. I want to store more than 10 objects in a remote server? Which methodology will follow?
A.?

Q. Can I modify an object in CORBA?
A.?

Q. How to communicate 2 threads to each other?
A. Basically, there are two general approaches to thread communication:
1. Shared memory
2. Event/queue based

In the shared memory approach, you might create a synchronized list or a synchronized map that both threads may read from and write to. Typically there is some overhead to making sure reads and writes occur without conflicts, you don't want to have an object you're reading deleted while you're reading it, for instance. Java provides collections that are well behaved, like Collections.synchronizedMap and Collections.synchronizedList.

In the event, or queue-based, thread communication, threads have incoming queues and write to other thread's incoming queues. In this scenario, you might have the heartbeat thread load up a queue with clients to read from and have the other thread poll/take from this queue and do its processing. The heartbeat thread could continually add the clients that are alive to this queue so that the processing thread "knows" to continue processing them.

Q. Explain RMI Architecture?
A. RMI, Remote Method Invocation is an alternate to RPC, used to invoke the methods (often called as Services) provided by an object of One JVM in another JVM.

This is the Remoting Technology that is supported by Java.
This technology can be used in the case of Java to Java only.

1. Whenever you use RMI, we need to develop the Remote Interface, A class that implements that Remote Interface.

2. We need to register the Object in a Registry.

3. Start the Server.

4. Lookup for the object in the registry and get the object.

The following is the Architecture of the RMI:

1. RMI consists of the following components:
a. Server: This JVM contains an object that provides remote services. It registers its object into the Registry.
b. Stub: This is generated in the server. This is used to understand or translate the request and response formats of the server and client in the server.
c. Client: This is the program that accesses the RMI Registry for the services of the server.
d. Skeleton: This is a class that remains in the Client and helps the client to understand the request and response formats of the server in the client.
e. RMI Registry: This is a Naming Registry that is used to bind the objects and return their references to the client.

Note: Whenever a client request for the object's reference, logical reference of the stub is returned but not the actual reference of the object.

Q. What is the use of Servlets?
A. servlets are the server-side component.
it acts as a controller.
it is used for request delegation too.
or
servlets can handle multiple requests concurrently and can synchronize requests. This allows a servlet to support systems such as online processing.

a servlet can forward requests to other servers or servlets.

Thus servlets can be used to balance load among several servers that mirror the same content and to partition a single logical service over servers according to task type or organizational boundaries.

Q. What is the difference between Process and Threads?
A. The process has its own memory space, runtime environment, and process Id, which means it creates its own shell thread runs inside a Process and shares its resources with other threads.

Q. What is the JAR file?
A. A JAR (for Java Archive) is an archive file format typically used to aggregate many Java class files and associated metadata and resources (text, images and so on) into one file to distribute application software or libraries on the Java platform

Q. What is JINI?
A. A network architecture for the construction of distributed systems.

Q. How will you call an Applet from a _Java Script?
A. 
<applet code="InJava.class" name="myApplet" height="100" width="100">
</applet>

Q. What is bean? Where it can be used?
A. They are used to encapsulate many objects into a single object (the bean), so that the bean can be passed around rather than the individual objects. ...

Q. Write down how will you create a binary Tree?
A. You first should create a class to store your data and implement Comparable or use a Comparator.
public class Data { // Implement Comparable...
private String s;
private int n1;
private int n2;

// Implement constructors, getters, setters based on what you need...

// Implement compareTo (+ equals + hashCode) unless your going with Comparator
}
Then use a Collection that implements SortedSet to store your data, TreeSet is a good choice. The objects in the SortedSet are stored by reference so if you modify a value set in a local variable it will be modified in the collection as well.

Edit: If I understood your question about reference-based lists correctly the following is possible in Java.
List dataList = // Create list and add data into it.
Data data = dataList.get(4);
data.setS(103); // Modifies S in the local data-object and in dataList because they are reference based.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.