Useful JAVA interview Questions


Ques.1>How an Abstract class can be defined?


Ans>A class having abstract method (i.e. not having a definition/body) is termed Abstract class and they cannot be instantiated or simply we cannot create object of this classes.

Eg.abstract class testAbstrctClass

{
protected String allString;
public String getAllString()

{
return allString;
}
public abstract string anyAbstrctFunc();
}

Ques.2>Define an Interface?

Ans>Interface defines the methods but does not implement them. A class that implements them is bound to implement all the methods defined in the interface.

For example: public interface testInterface

{
public void func1();

public void funs2();

}

Ques.3>In how many ways we can create an object? Explain with example.

Ans.>When we create a class, we are creating a new user data type and using this type we create objects. This can be done in one way i.e. using 'new’ operator which dynamically allocates memory for an object and returns a reference i.e. memory address to it.

For example: class_name x=new class_name;

Where, class_name is the class name and x is the object.


Ques.4>In Real time when do we go for Abstract classes and when do we go for Interfaces? How a class implements an interface?

Ans> When we don’t want to reveal the implementation logic of the method we use interface else we can use abstract class which consists of some implemented and some unimplemented methods.

The class must define all of the methods in the interface in its own way and a class can use an interface by the implements clause like

ClassA implements InterfaceTest{….body}

Ques.5>What is Collection API?

Ans>The Collection API as the term suggests is a group of interfaces along with classes all together supporting operations on collections of objects. If they are used properly they can be more flexible, powerful and regular than arrays, vectors and hash tables.

E.g. of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.

E.g. of interfaces: Collection, Set, List and Map.

Ques.6>What is the meaning of supplying String type arguments to main method?

Ans.>String type argument declares a parameter named args, which is an array of instances of the class String. Objects of type String store character strings. In some scenario direct input is required from the OS level by the user which can be done by this as it incorporates the use of command line argument. The syntax is given below:

Suppose to call a program named traceIP with a command line argument as 178.23.45.89

We will call it as: traceIP 178.23.45.89 (then enter)

Ques.7>What should happen if the same access specifier repeats more than once within a class? What modifiers are allowed for methods in an Interface?

Ans.>By the help of encapsulation we can control the access of the members of the class. If we use same access specifier for classes in the same program then we might have some problem but it is not the same if all the methods in a particular class use the same.

We can use public and abstract modifiers for the methods in an interface.

Ques.8>What is the difference between unchecked and checked Exceptions?

Ans.>All primitive Java exceptions are either a checked or unchecked exception. Both are defined in java.lang. Unchecked exceptions are those which do not check to see if a method handles and throws the exceptions. Checked exception are those which can generate one of the exceptions but does not handle by it itself.

E.g. of unchecked exception: ArithmeticException, ArrayStoreException.

E.g. of checked exception: ClassNotFoundException, InterruptedException.

Ques.9>What is method overriding?

Ans.>When a method in a subclass has the same name and signature as a method in its baseclass, then the method in the derivedclass is said to override the method in the baseclass. Method overriding occurs only when the names and the signatures of the two methods are identical; if they are not then they are simply overloaded. It is another way by which polymorphism is implemented by java.


Ques.10>what is synchronization and why is it important?

Ans.>Synchronization is the capability to control the access of multiple threads to common resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value, which might lead to some erroneous results. Synchronized method enables us to write very clear multithreaded code as it is an inbuilt Java feature.

Ques.11>What is serialization?

Ans.>The process of writing a byte stream state is called as serialization. This is useful when we want to save the state of our program to a persistent storing area. These objects may be restored later using the process of deserialization. Remote Method Invocation (RMI) is needed to implement which in turn will allow a Java object on one machine to invoke a method of a Java object on a different machine. The sending machine serializes the object and transmits it and the receiving machine desterilizes it.

Ques.12>What is finalization and what is its purpose?

Ans.>Sometimes an object will need to perform some action when it is destroyed, to handle such situation; Java provides a mechanism called ‘finalization’. By using finalization, we can define specific actions that will occur when an object is just about to be cleaned by the gc or garbage-collector. The runtime of Java calls this method whenever it is about to clean an object of that class.

For example:-

protected void finalize()

{

//enter your finalize codes in here

}

Ques.13>What is gc or garbage collection and how can you force garbage collection?

Ans.>In order to clean objects and free memory space, Java handles this de-allocation technique automatically using the gc or garbage collection. When an object is idle and not referring to any memory/references, that object is no longer needed; gc frees such memory space by de allocating those objects.

We can run the garbage collector in our code by calling the gc() method. Better way is to try to call gc() first and then call freeMemory() to get a proper memory usage.


Ques.14>What is method overloading?

Ans.>When two or more methods within the same class having different signatures but share the same name, then in that case the methods are said to be overloaded, such process is termed as method overloading. This is how Java implements static polymorphism.

E.g. class class_name {

void method_name (){

//method logic

}

void method_name (parameters){

//method logic

}

}

0 Response to "Useful JAVA interview Questions"

Post a Comment

Powered by Blogger