wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mainly  about   main( ) .... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mainly  about   main( ) ...." Watch "mainly  about   main( ) ...." New topic
Author

mainly about main( ) ....

vidya
Ranch Hand

Joined: Aug 08, 2000
Posts: 47
Hi...
pl help me with these qns..these are from javaprepare.com
1.What would be the results of compiling and running the
following class. Select the one correct answer.
class test {
public static void main() {
System.out.println("test");
}
}

A.The program does not compile as there is no main method
defined.
B.The program compiles and runs generating an output of "test"
C.The program compiles and runs but does not generate any
output.
D.The program compiles but does not run.

I thought the compiler must complain 'wrong number of arguments in main()' because main() takes an array of String as argument..But that is not one of the options, and the answer is given D.
cud somebody explain this ?

2.Which of the following are valid declarations for the main
method. Select all correct answers.
A. public static void main(String args[]);
B. public static void main(String []args);
C. final static public void main (String args[]);
D. public static int main(String args[]);
E. public static abstract void main(String args[]);
My answers are A and B , The answers given are A,B and C.Could somebody explain this?
This seems to be quite vague to me.Cud someone explain the legal access modifiers of the main() method? And , I remember having read in an earlier post here that the modifier private in the main method will not give any error ,I am not clear how that is possible too..
Thanks in advance,
Vidya.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Vidya,
The first program compiles but does not run. It compiles because the given method is valid and main is a valid method name. To run the program a method with the signature
public static void main(String[] whatever)
is required which is not defined in the class.
-Vani
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Yes, Vani got it right.
The famous public static void main{String whatever[] ) is the entry point of any Java class file. This is the necessary and sufficient condition that must be met if you want your program to run. However, if your class declares any other version of main() method whose signature differs from the one mentioned above, it is plain old overloading. It is perfectly legal and compiler has no problems with it.
Also, adding final qualifier to the standard main method signature doesnt seem to affect it. It is true that it runs quite fine with private or default modifer too. SUN has acknowledged this inconsistency but they feel it is not a bug and it is not going to get fixed. However, for the sake of SCJP, the acceptable signature must have public access modifier.
Look out for this kind of gotchas in the exam.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
So, can we select c also for SCJP???
And if any string value is to be written
is textfield, should the string be enclosed
in "".
[This message has been edited by avn (edited August 23, 2000).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
avn,
Yes, for the second question about the main function, you can select the option 'C'. Adding the final qualifer to main method is not illegal and does not change the behaviour of the method. Nothing precludes you from doing so.
For fill-in answers, do not enclose them in quotes, unless the question asks you to do so explicitly.
Hope this helps,
Ajith
vidya
Ranch Hand

Joined: Aug 08, 2000
Posts: 47
Thanks Vani,avn and Ajith ..
:-)
regards,
Vidya.
 
 
subject: mainly about main( ) ....
 
Similar Threads
dear all, i created a set of mock exam, please try it.
inner class
questions from java prepare
4 quickie questions . . . test yourself
Help me please