Java Interview Questions - More..

Started by sukishan, Jul 15, 2009, 01:41 PM

Previous topic - Next topic

sukishan

1. Q: What if the main method is declared as private?

    A: The program compiles properly but at runtime it will give "Main method not public." message.

2. Q: What if the static modifier is removed from the signature of the main method?

    A: Program compiles. But at runtime throws an error "NoSuchMethodError". 
3. Q: What if I write static public void instead of public static void?

    A: Program compiles and runs properly. 

4. Q: What if I do not provide the String array as the argument to the method?

    A: Program compiles but throws a runtime error "NoSuchMethodError". 
 
5. Q: What is the first argument of the String array in main method?

    A: The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the     program name.
 

6. Q: If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?

    A: It is empty. But not null.
   

7. Q: How can one prove that the array is not null but empty using one line of code?

    A: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
A good beginning makes a good ending