Skip to content
 

Java job interview questions and answers part 1

Core Java interview Questions with Answers

May 11 2009

Word count- 1000 (counted in MS-WORD) excluding Headings and other information.

Ques.1>How do you define an Abstract class?

Ans>A class containing abstract method(i.e. they don’t have a body) is called Abstract class, which cannot be instantiated.

Eg.abstract class testAbstractClass

{
protected String myString;
public String getMyString()

{
return myString;
}
public abstract string anyAbstractFunction();
}

Ques.2>How to define an Interface?

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

Eg.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 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 to it.

Eg.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? What must a class do to implement an interface?

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

The class must provide all of the methods in the interface and identify the interface in its implements clause.

Ques.5>What is Collection API?

Ans>The Collection API is a group of classes and interfaces that support operation on collections of objects. They are more powerful, flexible and regular than arrays, hash tables and vector if effectively used.

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 args[] to main method?

Ans.>String args[] declares a parameter named args, which is an array of instances of the class String. Objects of type String store character strings. In this, args receives any command-line arguments present when the program is executed. The syntax is given below:

public static void main (String args[])

This is the mechanism through which the runtime system passes information to our application.

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

Ans.>Through 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 checked and unchecked Exceptions in Java?

Ans.>All predefined exceptions in Java 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 and does not handle 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 type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. Method overriding occurs only when the names and the type signatures of the two methods are identical; if they are not then they are simply overloaded. It is another way that Java implements the `one interface, multiple methods` aspect of polymorphism.

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

Ans.>Synchronization is the ability to control the access of multiple threads to shared 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 and concise multithreaded code, because synchronization support is built in to the java language.

Ques.11>What is serialization?

Ans.>It is the process of writing the state of a byte stream. This is useful when we want to save the state of our program to a persistent storage area such as file. These objects may be restored later using the process of deserialization. It is needed to implement Remote Method Invocation (RMI), which allows 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 reclaimed by the garbage collector. The Java runtime calls that method whenever it is about to recycle an object of that class.

For example:-

protected void finalize()

{

//finalize code here

}

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

Ans.>In order to destroy object and release memory, Java handles this deallocation automatically using the technique garbage collection. When no reference to an object exists, that object is no longer needed; the memory occupied by it can be reclaimed again.

We can run the garbage collector on demand by calling the gc() method. A good way is to try to call gc() and then call freeMemory() to get a baseline memory usage.

Ques.14>What is method overloading?

Ans.>When two or more methods within the same class share the same name but having different parameter declarations, then in that case the methods are said to be overloaded and the process is referred to as method overloading. This is one of the ways that Java implements polymorphism.

E.g. class class_name {

void method_name (){

Body of the method}

void method_name (parameters){

Body of the method}

}





 


what should happen if the same access specifier repeats more than once within a class What should happen if the same access specifier repeats more than once within a class?what modifiers are allowed for methods in an interface?

Leave a Reply