adbrite adds

Your Ad Here

Java J2EE Best Answer

Read all the question and answer in the post and success the Interview.


search for Interview questions and job

Monday, August 25, 2008

Java Variable questions

Vaiables in java



1. What is the difference between class variable, member variable and automatic(local) variable?

Class variable is a static variable and does not belong to instance of class but rather shared across all the instances
member variable belongs to a particular instance of class and can be called from any method of the class
automatic or local variable is created on entry to a method and has only method scope

2. What are transient variables?
A transient variable is not stored as part of objects persistent state and they cannot be final or static. Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.

3. What are volatile variables?

It indicates that these variables can be modified asynchronously.

4. When are automatic variable initialized?

Automatic variable have to be initialized explicitly.

5. What types of values does boolean variables take?

It only takes values true and false.

6. Which primitive datatypes are signed?

All except char and Boolean
7. How is an argument passed in java, by copy or by reference?

If the variable is primitive data type then it is passed by copy.
If the variable is an object then it is passed by reference

8. Can abstract modifier be applied to a variable?

No, It is applied only to class and methods.

9. What is the difference between parameters and arguments?

While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

10. Why we should not have instance variable in an interface?

Since all data fields and methods in an Interface are public by default, then we implement that interface in our class then we have public members in our class and this class will expose these data members and this is violation of encapsulation as now the data is not secure.

11. What happens if you don’t initialize an instance variable of any of the primitive types in Java?

A: Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a boolean will be initialized to false.

12. What will be the initial value of an object reference which is defined as an instance variable?

The object references are all initialized to null in Java. However in order to do anything useful with these references, you must set them to a valid object, else you will get NullPointerExceptions everywhere you try to use such default initialized references.

13. What will be the default values of all the elements of an array defined as an instance variable?

If the array is an array of primitive types, then all the elements of the array will be initialized to the default value corresponding to that primitive type. e.g. All the elements of an array of int will be initialized to 0, while that of boolean type will be initializedto false. Whereas if the array is an array of references (of any type), all the elements will be initialized to null.

Friday, August 22, 2008

Some questions of Static in java

Static in java

1. Why is the main method static?

So that it can be invoked without creating an instance of that Class.

2. What is the difference between static and non-static variables?

A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

3. What is the difference between a static and a non-static inner class?

A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.

4. Where can static modifiers are used?

Static modifiers applied to variables, methods and a block of code, static methods and variables are not associated with any instance of class.

5. When are the static variables loaded into the memory?

During the class load time

6. When are the non static variables loaded into the memory?

They are loaded just before the constructor is called.

7. How can you reference static variables?

Via reference to any instance of the class
Computer is a class name.
We call the static variable , static method as
Computer.harddisk harddisk is variable of the class Computer
Computer.compute() computer() is a method of the class Computer


8. Can static method use non static features of there class

No, they are not allowed to use non static features of the class; they can only call static methods and can use static data.

9. What is static initializer code?

A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g.
public class Demo{
static int =10;
static{
System.out.println(“Hello world’);
}
}

And this code is executed exactly once at the time of class Load

10. Can inner classes be static?

Yes, inner classes can be static, but they cannot access the non static data of the outer classes.

11. What does it mean that a method or field is "static"?

A: Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class.
Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That's how library methods like System.out.println() work out is a static field in the java.lang.System class.

12. Can main method be declared final?

Yes, the main method can be declared final, in addition to being public static.

13. What are the static fields & static Methods ?

If a field or method defined as a static, there is only one copy for entire class, rather than one copy for each instance of class. static method cannot accecss non-static field or call non-static method. Static method can’t Override.

14. Why are the methods of the Math class static?

So they can be invoked as if they are a mathematical code library.

15. When are static and non static variables of the class initialized?

The static variables are initialized when the class is loaded. Non static variables are initialized just before the constructor is called

Thursday, August 21, 2008

Core Java Interview question 5

1. 1. How are Observer and Observable used?
Observer is a interface, Observable is a class. 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.

2. 2. What will be the default values of all the elements of an array defined as an instance variable?
If the variable is an array of primitive types, then all the elements of the array will be initialized to the default value corresponding to that primitive type. e.g. All the elements of an array of int will be initialized to 0, while that of boolean type will be initialized to false. Whereas if the variable is an array of references (of any type), all the elements will be initialized to null.

3. What happens if you don’t initialize an instance variable of any of the primitive types in Java?
Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a boolean will be initialized to false.

4. What will be the initial value of an object reference which is defined as an instance variable?

The object references are all initialized to null in Java. In program you must set a valid object, other wise you will get NullPointerExceptions.

5. What are the scopes for Java variables?

There are three types of scopes are:
1. Instance variable : - These are typical object level variables, they are initialized to default values at the time of creation of object, and remain accessible as long as the object accessible.
2. Local variable : - These are the variables that are defined within a method. They remain accessbile only during the course of method excecution. When the method finishes execution, these variables fall out of scope.
3. Static variable: - These are the class level variables. They are initialized when the class is loaded in JVM for the first time and remain there as long as the class remains loaded. They are not tied to any particular object instance.

6. What is the default value of the local variables?

The local variables are not initialized to any default value If you try to use these variables without initializing them explicitly, the java compiler gives an error in compile time.

7. some not a keyword in Java?

Main, next, delete, exit, No, main() is a Static Method.

8. What is the Locale class?

The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region.

9. What is the purpose of finalization?

The purpose of finalization is to cleanup the unused java object.

10. What is the difference between preemptive scheduling and time slicing?

Under preemptive scheduling, the highest priority task executes. Under time slicing, a task executes for a predefined slice of time and then execute next task.

11. What is garbage collection ?

The runtime system keeps track of the memory that is allocated and is able to determine whether that memory is still useable. This work is usually done in background by a low-priority thread that is referred to as garbage collector. When the gc(System.gc()) finds memory that is no longer accessible from any live thread it takes steps to release it back to the heap for reuse.

Monday, August 18, 2008

Java Interview Question 4

1.Q: Does Java provide any construct to find out the size of an object?

A: No,there is not a direct way to determine the size of an object directly in Java.

2.Q: How we get the execution time of the process?

A: Read the system time just before the method is invoked and immediately after method returns. Take the time difference, which will give you the time taken by a method for execution.
To put it in code...

long start = System.currentTimeMillis ();
method ();
long end = System.currentTimeMillis ();

System.out.println ("Time taken for execution is " + (end - start));

Remember that if the time taken for execution is too small, it might show that it is taking zero milliseconds for execution. Try it on a method which is big enough, in the sense the one which is doing considerable amout of processing.

3.Q: What are wrapper classes?

A: Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are Integer, Character, Double, Float etc.

4.Q: Why do we need wrapper classes?

A: It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.

5.Q: What are checked exceptions?

A: Checked exception are those which the Java compiler forces you to catch. e.g. IOException are checked Exceptions.

6.Q: What are runtime exceptions?

A: Runtime exceptions are those exceptions that are thrown at runtime.The exception comes because of wrong input data etc.

7.Q: What is the difference between error and an exception?

A: An error is an irrecoverable condition occurring at runtime. Eg. Wrong input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception

8.Q: How to create user defined exceptions?

A: our myexception class should extend the class Exception. Then we use the custom exception to our program.

9.Q: If I want an object of my class to be thrown as an exception object, what should I do?

A: The class should extend from Exception class. Or you can extend your class from some more precise exception type also.


11.Q: If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object?

A: One can not do anytihng in this scenarion. Because Java does not allow multiple inheritance and does not provide any exception interface as well.

12.Q: How does an exception permeate through the code?

A: An unhandled exception moves up the method stack in search of a matching When an exception is thrown from a code which is wrapped in a try block followed by one or more catch blocks, a search is made for matching catch block. If a matching type is found then that block will be invoked. If a matching type is not found then the exception moves up the method stack and reaches the caller method. Same procedure is repeated if the caller method is included in a try catch block. This process continues until a catch block handling the appropriate type of exception is found. If it does not find such a block then finally the program terminates.

13.Q: What are the different ways to handle exceptions?

A: There are two ways to handle exceptions,
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and
2. List the desired exceptions in the throws clause of the method and let the caller of the method hadle those exceptions.


14.Q: What is the basic difference between the 2 approaches to exception handling.
1> try catch block and
2> specifying the candidate exceptions in the throws clause?
When should you use which approach?

A: In the first approach as a programmer of the method, you urself are dealing with the exception. This is fine if you are in a best position to decide should be done in case of an exception. Whereas if it is not the responsibility of the method to deal with it's own exceptions, then do not use this approach. In this case use the second approach. In the second approach we are forcing the caller of the method to catch the exceptions, that the method is likely to throw. This is often the approach library creators use. They list the exception in the throws clause and we must catch them. You will find the same approach throughout the java libraries we use.

15.Q: Is it necessary that each try block must be followed by a catch block?

A: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

16.Q: If I write return at the end of the try block, will the finally block still execute?

A: Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.

17.Q: If I write System.exit (0); at the end of the try block, will the finally block still execute?

A: No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.

Study JAVA J2EE

Read EJB Session Bean Guide post and do EJB with a hour BEST OF LUCK CONTACT 9942291292

click to get job