This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why the code can Watch "why the code can New topic

why the code can't be compiled

nick magic

Joined: Mar 03, 2006
Posts: 11
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: 37
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.

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

Joined: Nov 07, 2005
Posts: 37
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 Khvatov
Ranch Hand

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

Joined: Feb 07, 2005
Posts: 2398
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!
I agree. Here's the link:
subject: why the code can't be compiled
It's not a secret anymore!