What are the environment variables do we need to set to run Java ?

We need to set two environment variables – PATH and CLASSPATH.

Can you serialize static fields of a class ?

Serialization is for serializing instances, not classes. Static fields or methods are irrelevant since they are part of the class definition so they are not serialized and will be reinitialized to whatever value they are set to when the class is loaded. If you have any mutable static field, then the changes made to that value will be lost.

What is the difference between declaring a variable and defining a variable ?

In variable declaration we mention only the type of the variable and its name. Therefore, it does not have any reference to live object.

In variable definition we mention the type, name and initialization of the variable. It has a reference to the live object.

For example,

List<String> list; //declaration
List<String> list = new ArrayList<String>(); //definition

Where can we use serialization ?

If we want to send an object or piece of data over the network we use streams of bytes.
If we want to save some data to the disk we again use the binary mode along with the streams of bytes and save it.
We are putting all the bytes of our object together in order to store them and be able to convert them to get back the original object later.
In General, serialization is a method to persist an object’s state.

What modifiers are allowed for methods in an Interface ?

Only public and abstract modifiers are allowed for methods in an interface.

What is the purpose of Runtime and System class ?

The purpose of the java.lang.Runtime class is to provide access to the Java runtime system. The runtime information – memory availability, invoking the garbage collector are possible by using Runtime class.

Some other purposes provided:

Writing to console output
Reading from the keyboard as input
Interaction with JVM process
Reading/writing system properties/environment variables
Executing other programs from within java apps

The purpose of the java.lang.System class is to provide access to system resources. Standard input,output and error output streams are provided with System class. These are used to access the externally defined properties and environment variables.

Some other purposes provided:

Access to external properties
A way of loading files and libraries
A utility method for quickly copying a portion of an array

What is the difference between static synchronized and synchronized methods ?

Static synchronized methods synchronize on the class object. That means while execution of a static method the whole class is blocked. So other static synchronized methods are also blocked.

Non-static synchronized methods synchronize on this, i.e., the instance of the class. When you use the synchronized modifier on an instance method (a non-static method), it is very similar to having a synchronized block with “this” as the argument.

What is the order of catch blocks when catching more than one exception ?

When dealing with multiple catch blocks, it is necessary to specify exception sub classes first, then followed by exception super classes. Otherwise compile time error will be raised.

What is the difference between the prefix and postfix forms of the increment(++) operator ?

The prefix form of increment operator first performs the increment operation and then returns the value of the increment operation of the variable.

int counter = 1;



The postfix form of increment operator first returns the current value of the increment operation of the variable and then performs the increment operation on that value.

int counter = 1;



What is hashCode ?

In the Java programming language, every class implicitly or explicitly provides a hashCode() method, which digests the data stored in an instance of the class into a single hash value that is a 32-bit signed integer.

What are the restrictions when overriding a method and overloading a method ?

Restrictions of overriding a method

Overridden method must have the same name, parameter list, and same return type.
The access level must not be more restrictive than that of the overridden method.
The access level can be less restrictive than that of the overridden method.
The overriding method must not throw new or broader checked exceptions.

Restrictions of overloading a method

Overloaded methods must change the argument list.
Overloaded methods can change the return type.
Overloaded methods can change the access modifier.
Overloaded methods can declare new or broader checked exceptions.
A method can be overloaded in the same class or in a subclass

What is the use of assert keyword ?

Java assertion feature allows developer to put assert statement in Java source code to help unit testing and debugging. Assert keyword validates certain expressions. It replaces the if block effectively and throws an AssertionError on failure.

For example,

assert(x >= 0 && x < 2);  // Ensures that `x` is between 0 and 1.
// Switch statement to follow
switch(x) {
    case 0:
        // do some stuff
    case 1:
        // do some other stuff
      throw new IllegalArgumentException("x is illegal");

What is adapter class ?

An adapter class provides the default implementation of all methods in an event listener interface. Adapter classes are very useful when you want to process only a few of the events that are handled by a particular event listener interface. For example MouseAdapter provides empty implementation of MouseListener interface. It is useful because very often you do not really use all methods declared by interface, so implementing the interface directly is very verbose.