Tuesday, 23 April 2013

Experienced Java/J2EE interview questions by MNC

Experienced Java/J2ee Interview questions asked by MNC.

1. Why main() in java is declared as public static void main? What if the main method is declared as

Ans : Because, every program start exucution from main function.The static method can directly call without createing the object of the class.So, before createing object the main function runs and then it creates object.

And, public in main method due to access by JVM. If the method is private then JVM cannot call that function.The program will compile but never run.It show the message  "Main method not public."

2.What is Externalizable?

Ans : This interface is used for serialization.To save the state of an object in file. It provides two method
readExternal() and writeExternal().

3.What modifiers are allowed for methods in an Interface?
Ans : abstract and public

4.What are the different identifier states of a Thread?

Ans :
R- Running or Runnable
S- Suspended
MS- Thread Suspended on Moniter lock
MW- Thread waiting on moniter
CW- Thread waiting on condition variable

5.What are some alternatives to inheritance?

Ans : Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).

6.Why isn’t there operator overloading?

Ans : Because C++ has proven by example that operator overloading makes code almost impossible to maintain.

7.What does it mean that a method or field is “static”?

Ans : Static method or field are member of class.They do not need any object for call or access . We can directly call the static method and fields without using the object.