Java Interview Questions and Answers, Java Interview Questions

What is Collection API ?
The Collection API is a set of classes and interfaces that substantiation deployment on collections of objects. These classes and interfaces are more flexible, additional 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.

Is Iterator a Class or Interface? What is its use?
Answer: Iterator is an interface which is used to step through the elements of a Collection.

What is similarities/difference between an Abstract class and Interface?
Differences are as follows:
Interfaces provide a fashion of multiple inheritance. A class can extend only one other class. Interfaces are limited to public supplies 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 lesson may produce simply one abstract class. Interfaces are slow as it requires surplus indirection to to attain corresponding method in in the actual class. Abstract classes are fast.
Similarities:

Neither Abstract classes or Interface can be instantiated.

Java Interview Questions - How to define an Abstract class?
A course containing abstract method is identified 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();
}

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

public interface sampleInterface {
public void functionOne();

public long CONSTANT_ONE = 1000;
}

If a class is located in a package, how do you need to change in the OS environment to be able to use it?

You need to add a register or a jar file which 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 technique main(), you could test it on a command prompt window as follows:
c:\>java com.xyz.hr.Employee

How a multitude of methods in the Serializable interface?
There is no method in the Serializable interface. The Serializable interface acts as a marker, telling the object serialization ideas that your class is serializable.

How many methods in the Externalizable interface?
There are two methods in the Externalizable interface. You suffer to start these kinds of two resources in order to make your lesson externalizable. These two resources are readExternal() and writeExternal().

What is the difference between Serializalble and Externalizable interface?
When you use Serializable interface, your class is serialized automatedly by default. But you can override writeObject() and readObject() two resources to control more complex object serailization process. When you use Externalizable interface, you have a thorough control over your class's serialization process.

What is a transient variable in Java?
A transient variable is a variable the current may not be serialized. If you do not want some field to be serialized, you can mark that field transient or static.


Which containers use a periphery layout as such a default layout?
The Window, Frame and Dialog classes use a border layout as the default layout.

How are Observer and Observable used?
Objects that subclass the Observable class keep up a list of observers. When an Observable object is updated, it invokes the update() method of every of its observers to say to the observers that it has amended state. The Observer interface is implemented by objects such a observe Observable objects.

0 comments:

Post a Comment