This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question 3 on javaprepare 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 "question 3 on javaprepare" Watch "question 3 on javaprepare" New topic
Author

question 3 on javaprepare

may leung
Greenhorn

Joined: Nov 28, 2000
Posts: 23
Hi all,
What gets printed when the following code is compiled and run with the following command -
java test 2
Select the one correct answer.

public class test {
public static void main(String args[]) {
Integer intObj=Integer.valueOf(args[args.length-1]);
int i = intObj.intValue();
if(args.length > 1)
System.out.println(i);
if(args.length > 0)
System.out.println(i - 1);
else
System.out.println(i - 2);
}
}

The ans is 1. Can anyone explain to me why?
Thanks!!!
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Step through the code.
1. You are getting the value of the first argument passed on the command line and passing it into an Integer object.
2. You take that value and return it as an int with intObj.intValue(). So i = 2.
3. You check to see if the length of args is greater than 1, which it is not since there is only one value ("2") passed in so length = 1.
4. You check to see if the length of args is greater than 0 which it is since a value was passed in, so you take i-1 which is 1
Bill
may leung
Greenhorn

Joined: Nov 28, 2000
Posts: 23
oh!!
I'm clear now
Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question 3 on javaprepare
 
Similar Threads
Integer??
Naveen's mock exam qn 3??
mock exam
Strange output
This is from Javaprepare.com