Interface Java Interview Questions – Set 01

Can an inner class be built in an Interface?

Yes,an inner class may be built an Interface.

public interface xyz

{

static int p=0;

void m();

class c

{

c()

{
int q;

System.out.println(“inside”);
}
public static void main(String c[])

{
System.out.println(“inside “);

}
};
}

 

Why is explicit object casting needed?

In order to assign a superclass object in a variable to a subclass,one needs to do explicit casting.

For example:Person person=null;

Man man = (Man)person;

An automatic casting takes place when we typecast a object in subclass as parent class object.

Define Externalizable.

Externalizable is coined as an Interface

It extends the Serializable Interface.

It also sends data into the Streams.

Externalizable sends data in a Compressed Format.

Externalizable is having two methods,for e.g. writeExternal(ObjectOuput out) & readExternal(ObjectInput in)

What are available drivers in JDBC?

JDBC is a set of Java API for executing SQL statements. This API consists of a set of classes and interfaces to enable programs to write pure Java Database applications.

JDBC technology drivers fit into one of four categories:

  • A JDBC-ODBC bridgeprovides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important. For information on the JDBC-ODBC bridge driver provided by Sun, see “http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/bridge.doc.html#996747” JDBC-ODBC Bridge Driver.
  • A native-API partly Java technology-enabled driverconverts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.
  • A net-protocol fully Java technology-enabled drivertranslates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all of its Java technology-based clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that all vendors of this solution will provide products suitable for Intranet use. In order for these products to also support Internet access they must handle the additional requirements for security, access through firewalls, etc., that the Web imposes. Several vendors are adding JDBC technology-based drivers to their existing database middleware products.
  • A native-protocol fully Java technology-enabled driverconverts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are proprietary the database vendors themselves will be the primary source for this style of driver. Several database vendors have these in progress.

a) JDBC-ODBC Bridge driver

b) Native API Partly-Java driver

c) JDBC-Net Pure Java driver

d) Native-Protocol Pure Java driver

Can you write code to sort the following string values naturally (i.e. in alphabetical order)? (JEE, Java, Servlets, JMS, JNDI, JDBC, JSP, and EJB)

Here is the sample code that makes use of the default compareTo( ) provided in the String class as it implements the Comparable interface and the Collections utility class that provides a sorting method, which internally uses the efficient “merge sort” algorithm.

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

public class Sort1 {

public static void main(String[] args) {

List<string> values = Arrays.asList(“JEE”, “Java”, “Servlets”, “JMS”, “JNDI”, “JDBC”, “JSP”, “EJB”);

Collections.sort(values); // uses the default compareTo(String anotherString)  in the String class

System.out.println(values);

}

}

Output:

[EJB, JDBC, JEE, JMS, JNDI, JSP, Java, Servlets]

How can a collection object be sorted?

// Sort

Collections.sort(list);

// Sort case-insensitive

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

// SortReverse-order

Collections.sort(list, Collections.reverseOrder ());

// Reverse-order sort case-insensitive

Define local, member and a class variable.

Within a method variables declared are called “local” variables.

Variables declared in the class i.e not in any methods are “member” variables (global variables).

Variables declared in the class i.e not in any methods and are called as “static” are class variables.

Name the different identifier states of a Thread.

Different types of identifiers of a Thread are:

R – Running or runnable thread

S – Suspended thread

CW – Thread waiting on a condition variable

MW – Thread waiting on a monitor lock

MS – Thread suspended waiting on a monitor lock

Define Vector class? Differentiate the Vector and ArrayList.

Vector canbe said a legacy class which has been introduced to implement the List interface since Java 2 platform v1.2

Vector is always synchronized but ArrayList is not.

When Vector class is synchronized, if we will run in multithreading environment we’ve to use ArrayList with Collections.

Vector has a default size i.e 10 while arrayList has no default size.

ArraayList is not having any method returning Enumerations where as vector list is having.

Differentiate between Enumeration and Iterator interface

In java.util package the Enumeration and Iterator are available.

The Enumeration interface is replicated by the Iterator interface.

In preference to Enumeration new implementations should consider using Iterator .

The difference of Iterators from enumerations are:

  • Enumeration has 2 methods namely hasMoreElements() & nextElement() where the Iterator contained three methods namely hasNext(), next(),remove().
  • An optional remove operation is added in Iterator,and has shorter method names. We Use remove() to delete the objects but the Enumeration interface does not support this feature.
  • The legacy classes use Enumeration interface .Vector.elements() & Hashtable.elements() method results Enumeration.
  • All Java Collections Framework classes returns iterator. java.util.Collection.iterator() method returning an instance of Iterator.

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

Collections.reverse(list);

When will you use Comparator and Comparable interfaces?

java.util.Comparator and java.lang.Comparable

java.util.Comparator compares some other class’s instances,

while java.lang.Comparable compares itself with another object.

What is RMI and how it is useful?

Remote method invocation is called RMI.

One can work with remote object using RMI.

It gives a impression that you are working with a object that resides within your own JVM though it is somewhere.

The protocol used by RMI is RMI-IIOP

Define a Collection API.

The set of classes and interfaces supporting the operation on collections of objects is the Collection API.

Than the vectors, arrays, and hashtables if effectively replaces,these classes and interfaces are more flexible, more powerful, and more regular

class examples: HashSet, TreeMap, ArrayList, LinkedList,HashMap and TreeMap.

interface examples: Set,List ,Collection and Map.

How many forms of Polymorphism are there?

polymorphism exists in three different forms in Java:

  • Method overloading
  • Method overriding through inheritance
  • Method overriding through the Java interface

Define the wrapper classes in Java and name a few.

Wrapper class is wraps around the primitive data type. List of the primitive types and the corresponding wrapper classes:

Primitive Wrapper

boolean java.lang.Boolean

byte java.lang.Byte

char java.lang.Character

double java.lang.Double

float java.lang.Float

int java.lang.Integer

long java.lang.Long

short java.lang.Short

void java.lang.Void

Differentiate between JDK ,JRE & JVM

JDK stands for Java Development Kit. It is the most widely used Java Software Development Kit.

JRE stands for Java Runtime Environment. It is an implementation of the Java Virtual Machine which executes Java programs

JVM stands for Java Virtual Machine. It is an interpreter.

What modifiers may be used with an interface declaration

An interface may be declared as public or abstract

What are the two basic ways in which classes that can be run as threads may be defined

A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface

How are Observer and Observable used?

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.