Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scjp help required

 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:
1. public class Alpha{
2. public static void main( string[] args ){
3. if ( args.length == 2 ) {
4. if ( args.[0].equalsIgnoreCase(�-b�) )
5. System.out.println( new Boolean( args[1] ));
6. }
7. }
8. }
And the code is invoked by using the command:
java Alpha �b TRUE
What is the result?
A. true
B. null
C. false
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: A
Why ??
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PETER, I have asked you before: please specify the source of these questions.

Why do you think the anwser is not choice A? Have you run the code?
 
s choukse
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is that you did not understand in this code. The code is pretty straight forward.
I am concidering that
1. The instead of was a typo.
2. The instead of was a typo.

To compile the code then true is an obvious answer.
If the above assuptions are incorrect and you got the code the way it is in your post then it will result in compilation error.

-Saurabh
[ September 15, 2004: Message edited by: Barry Gaunt ]
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,but I compile fail:
E:\l>javac Alpha.java
Alpha.java:4: <identifier> expected
if ( args.[0].equalsIgnoreCase("-b") )
^
Alpha.java:6: ')' expected
}
^
2 errors
Why I can't compile successfully?
Thanks .
 
Adrian Marti
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
args[0] not args.[0]

args is an array

hth

Adrian
 
Doyle Matt
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,

May I suggest the following books for you to get an indepth knowledge on java.

1. Beginning Java 1.4 Ed by Horton
2. Sun Certified Programmer & Developer for Java 2 Study Guide by Kathy Sierra, Bert Bates

There are a lot of online resources namely:

1. Javaranch (of course)
2. java.sun.com (try to download the tutorial)
3. javaworld.com
4. google.com

Hope this will solve all of your problems

Cheer.
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marti,

public class Alpha{
public static void main( String[] args ){
if ( args.length == 2 ) {
if ( args[0].equalsIgnoreCase("-b") )
System.out.println( new Boolean( args[1] ));
}
}
}
I compile it.
E:\l>java Alpha -b TURE
false
So C is right ,not A.
Do you follow me ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TURE? You typed TURE?
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E:\l>java Alpha -b TURE//wrong

E:\l>java test1 -b true
true

That's right !!

Thank you very much !!

Cheers !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic