Make your own free website on Tripod.com

Java FAQ

  1. What is the signature of the main method from Java 2.0 onwards?

Method

IsValid

public static void main(String[] args)

Valid

protected static void main(String[] x)

Valid

static void main(String args[])

Valid

private static void main(String [] args)

Valid

 

  1. How many primitive Types are there in Java?


There are a total of Eight primitive types in Java.
 
 

Keyword

Description

Size/Format

(integers)

 

Bytes

Bits

Byte

Byte-length integer

1

8-bit two's complement

Short

Short integer

2

16-bit two's complement

Int

Integer

4

32-bit two's complement

Long

Long integer

8

64-bit two's complement

(real numbers)

Float

Single-precision floating point

4

32-bit IEEE 754

Double

Double-precision floating point

8

64-bit IEEE 754

(other types)

Char

A single character

2

16-bit Unicode character

Boolean

A boolean value (true or false)

1

true or false

 

  1. What is the data type in java that supports Unicode?

char

 

  1. What are the default values for any object variable?

 

Data Type

Value

byte

0

short

0

int

0

long

0L

float

0.0f

double

0.0D

char

‘\u0000’

boolean

false

Any Object Reference

null

 

  1. Some Method overriding and Object casting scenarios?

class A {

int a = 1;

void x(){
}

}

class B extends A{

int a = 2;

void x(){

}
}

class C extends A{

int a = 3;

void x(){

}
}

                A
              /   \
             B     C

Scenario

IsValid

Notes

A a = new A();

B b = new B();

a.x();

b.x();

Legal

Calling a.x() will result in calling class A’s x method

And Calling b.x() will result in calling class B’s x method

A a = null;

B b = new B();

a = b;

a.x();

Legal

In this scenario calling a.x will result in calling B’s x method ‘cause A’s x method is being overridden by the B’s x method

A a = new A();

B b = null;

b = a;

Gives a compile time error

Cannot cast a parent class to subclass (No automatic conversion)

A a = new A();

B b = null;

b = (A)a;

Is legal at compile time but gives Runtime Exception – ClassCastException

The compiler cannot really check whether ‘a’ is an instance of A or B it cannot be only determined only at runtime. As ‘a’ is really an instance of A and thus cannot be casted down to a child class

B b = new B();

A a = b;

B b1 = a;

Gives a compile time error

Cannot cast a parent class to subclass (No automatic conversion)

B b = new B();

A a = b;

B b1 = (B)a;

Is legal at both compile time and runtime.

This time the object ‘a’ is really an instance of class B so casting down again to ‘b1’ does not give any problem

A a = null;

B b = null;

C c = new C();

b = c; (or) b = (B)b

Both b = c && 

b = (B)b are invalid & code will not compile

Casting of a different which does not fall into one single hierarchy is not legal both at compile and runtime.

A a = null;

B b = null;

C c = new C();

a = c;

b = (B)a;

Legal at compile time but not at runtime will give ClassCastException

Point of failure is 

b = (B)a, as ‘a’ is not an instance of A but C

 

  1. What are the differences between Interfaces and abstract classes?

Interfaces

Abstract Classes

interface A {

int i=1;

Void x();

}

abstract class A{

int i=1;

abstract void x();

void y(){

}

}

Interfaces can have only methods that are by abstract in nature. (Compiler by default puts the keyword abstract for method x())

Methods in abstract class should be explicitly declared as abstract if they need to be abstract.

All members of the interface are by nature public even though we do not declare them as public.

Each method needs to be declared accordingly whether it is public, private or protected. By default the access modifier is <default> (no modifier/declaration) which specifies the accessibility as package level.

All variables are by default public static final

Again each variable needs to be declared accordingly and by default they specify package level accessibility.

Interfaces cannot have methods, which have implementation.

There is No requirement for an Abstract class to have atleast one abstract method but if a class is having atleast one abstract method then the class must be declared as abstract.

 

  1. About ‘final’ variables?

Scenario

IsValid

class A{

final int x = 1;

}

Valid

class A{

final int x;

}

Not valid, final variables must be assigned a value.

class A{

final int x;

A(){

String st = "Hello";

x = 1;

}

}

Valid, final variables can be assigned a value in constructor, but only once.

class A{

final int x;

A(){

String st = "Hello";

x = 1;

}

A(int abc){

String st = "Hello";

x = 2;

}

}

You always make sure that if you have n number of constructors, any final variable if not initialized must be initialized in each and every constructor, if you miss to initialize the final variable in atleast constructor, compilation will result in an error.

class A{

final static int x;

A(){

String st = "Hello";

x = 1;

}

}

This type of assignment is not valid if a variable is final and static.

class A{

final static int x = 1;

A(){

String st = "Hello";

}

}

Valid

class A{

final static int x;

static {

x = 1;

}

A(){

String st = "Hello";

}

}

Valid, A final static variable should be either assigned a value instantly or can be assigned a value in & only in a static initializer.


 

  1. What’s the Core Exception handling API that Java provides the developers?
                  java.lang.Object
                           |
                  java.lang.Throwable
                           |
            ---------------------------------
            |                                |
      java.lang.Error               java.lang.Exception
            |                                |
                             ---------------------------------
                            |                                 |
                                                 java.lang.RuntimeException
  1. What’s Runtime exception and Compile time exception?

Any exception occurs at runtime, but the exceptions are categorized as runtime and compile time because of their nature and how the code is being designed.

Any class which extends from Throwable or Exception is a Compile time exception, except the RuntimeException which is a subclass of Exception.  For example: if a method

Is declared throws Exception or Subclass of Exception NOT RuntimeException, Any other class or method using that method should either catch that exception or again Declare that method as throws that particular Exception, failing to do so will result in a compilation error.

 

Scenario

IsValid

class A{

 void A()throws SubclassOfException{
 }

}

Valid

class A{

void A() {

   throw new Subclass OfException();

}

}

Not valid, Either the exception should be caught or the method A should declare as throws SubclassOfException

class A{

void A() {

  throw new SubclassOfRuntimeException();

}

}

Valid, RuntimeExceptions need not be caught but has to be elegantly dealt with.

class A{

      void x() throws Exception {

      }

}

 

class B extends A{

      void x() throws TestException {

      }

}

 

class TestException extends Exception{

}

Valid, The over riding method void x() in class B can throw an Exception which is a subclass of the Exception which the overridden method is throwing. Moral, the overriding method MUST not throw an exception that is different from the overridden method.



  1. Is a java Thread platform Independent? Is the Execution nature of Java Thread platform independent?

(Yes and No).  Since Java VM had been ported to variety of operating systems, and each operating system has its own way of executing threads. Java uses the underlying operating systems native threads mostly, so execution nature of the java thread differs from platform to platform.

  1. What is the difference between Java Streams and Readers/Writers?

Java Readers/Writers have been introduced from JDK1.1 onwards, to support UNICODE character set storage and retrieval from storage devices. The java Input/OutPut Streams are by nature Byte Oriented where as Readers and Writers are character oriented which is a two byte.

  1. What is serialization? How do you make a Java class Serializable?

Serialization is  a process of storing Java Object into a persistent storage device, which can be a file, network, or any device etc. To make a java Object serializable, it needs to implement interface java.io.Serializable.

  1. What are necessary precautions you need to take when you make a Java class as serializable?

When you design/develop a java class which implements java.io.Serializable interface all the member variables must be either primitive types or  an  instance of a class which implements serializable interface. Else the when you try to save the object to a persistent storage device the java Virtual Machine throws a NotSerializableException the moment it encounters a Non Serializable member variable in the class. To get around this problem the member variable must be declared as transient.

  1. What are different types of JDBC Drivers does Java offer?

         I.      A JDBC-ODBC bridge provides 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.

       II.      A native-API partly Java technology-enabled driver converts 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.

     III.      A net-protocol fully Java technology-enabled driver translates 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.

  IV.      A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protcol 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.

  1. Given a resource in a WebServer such as a HTML Document, images etc., on a WebServer how do you access them using a standard API?
  2. How do you send some input to a CGI script (Such as a Java Servlet, PERL module, C module) on a Webserver and read the output of the same? (Hint: what’s the Java API that supports this mechanism and how?)
  3. What’s the difference between HTTP GET method and POST method?
  4. What are the advantages of a Java Servlets over standard CGI?
  5. What’s the life cycle of a Servlet?
  6. What are the bare minimum requirements to develop a Java Servlet?
  7. What are the different techniques you implement to maintain a persistent session between your Web application and the Users Web Browser?
  8. How does java HttpSession work? Or what is it’s underlying mechanism?
  9. What is the Java Interface that support interacting with other resources such as Servlets/JSP? (Hint: Servlet 2.2 API).
  10. How does the JSP Technology work?
  11. What are the advantages of JSP over Servlets?
  12. In a Web Application scenario what are the primary roles of a Servlet, JSP, Java Beans etc?
  13. What is Java RMI and what is the architecture of RMI?
  14. Explain the various components of J2EE Technology? (Take your own time ;-) )
  15. What is the common interface the Session Bean and Entity Bean implement?
  16. What is the difference between Stateless and Stateful Session Bean? Architecturally where does they fit in an Application?
  17. What are the subtle differences between Bean Managed and Container Managed Entity Beans? What are the advantages of one over the other?
  18. What is the single transaction API that the J2EE supports to do your custom Transactions?
  19. What’s the role of XML as a technology in J2EE Architecture? How does XML enables your EJB components (Which are Confirmed to EJB 1.0/1.1/2.0 Specification) deployable across Application servers?
  20. What is the difference between XML and HTML?
  21. What is a well formed and a valid XML document?
  22. What are the advantages of XML based protocols?
  23. What are the advantages of XML in Business to Business transactions?
  24. Can you compare an Object, Database, XML document and how do you leverage these technologies in developing a Web application?