wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why the code can't be compiled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why the code can Watch "why the code can New topic
Author

why the code can't be compiled

nick magic
Greenhorn

Joined: Mar 03, 2006
Posts: 11
Given:
1. public class test {
2. public static void main(string[]args){
3. string foo = args [1];
4. string foo = args [2];
5. string foo = args [3];
6. }
7. }
And command line invocation:
C:\>java Test red green blue
What is the result?
A. Baz has the value of ��
B. Baz has the value of null
C. Baz has the value of �red�
D. Baz has the value of �blue�
E. Bax has the value of �green�
F. The code does not compile.
G. The program throws an exception.

The answer is G. But i test the code in the java1.5.0 run environment, when i compile the code, it reports the error: in the L4,L5, the foo have been defined
So i wonder wether i could choose F instead of G? thanks


la qualite de la vie!
Alex Khvatov
Ranch Hand

Joined: Nov 07, 2005
Posts: 36
The code would throw a runtime exception because the program refers to an unexisting array index. The command line arguments are represented by a zero based array, so if you're passing three elements to the program, the last one would be at the index two not three.
Alex


SCJP 5<br />SCWCD 5
Alex Khvatov
Ranch Hand

Joined: Nov 07, 2005
Posts: 36
Another correction. Here you're defining a 'string' which is not a correct name for a String class. The code would not comple because of 'string' and not 'String'.
Alex
Alex Khvatov
Ranch Hand

Joined: Nov 07, 2005
Posts: 36
... And you can't have multiple definitions of the same local variable.
Alex
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I think that's the point of the OP's question. The program won't compile so it can't throw an exception.
Thai Son Cung
Ranch Hand

Joined: Aug 25, 2005
Posts: 46
I think F is the correct answer. The program will be conpilation fail!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why the code can't be compiled
 
Similar Threads
a question about boolean?
Qestion about command line arguments.
question about classpath
Another java.ditmas problem