SAP Documents
SAP.

Saturday, June 21, 2008

Java Interview Questions - Answers Vol - 2

Q. What is Collection API ?

A. The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces. Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.Example of interfaces: Collection, Set, List and Map.


Q. Is Iterator a Class or Interface? What is its use?

A. Iterator is an interface which is used to step through the elements of a Collection.

Q. What is similarities/difference between an Abstract class and Interface?

A. Differences are as follows: Interfaces provide a form of multiple inheritance. A class can extend only one other class. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc. A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast. Similarities: Neither Abstract classes or Interface can be instantiated.

Q. How to define an Abstract class?

A. A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.Example of Abstract class:

abstract class testAbstractClass {

protected String myString;

public String getMyString()

{

return myString;

}

public abstract string anyAbstractFunction();

}

Q. How to define an Interface in Java ?

A. In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.Emaple of Interface:

public interface sampleInterface {

public void functionOne();

public long CONSTANT_ONE = 1000;

}

Q. If a class is located in a package, what do you need to change in the OS environment to be able to use it?
A. You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let's say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:c:\>java com.xyz.hr.Employee

Q. How many methods in the Serializable interface?

A. There is no method in the Serializable interface. The Serializable interface acts as a marker, telling the object serialization tools that your class is serializable.


Q. How many methods in the Externalizable interface?

A. There are two methods in the Externalizable interface. You have to implement these two methods in order to make your class externalizable. These two methods are readExternal() and writeExternal().

Q. What is the difference between Serializalble and Externalizable interface?

A. When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.

Q. What is a transient variable in Java?

A. A transient variable is a variable that may not be serialized. If you don't want some field to be serialized, you can mark that field transient or static.

Q. Which containers use a border layout as their default layout?

A. The Window, Frame and Dialog classes use a border layout as their default layout.

Q. How are Observer and Observable used?

A. Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated, it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

1 comment:

Unknown said...

Given are nice java questions. Similar to that javapapers.com has good collection of java faq.