aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Naveen's mock exam qn 3?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Naveen Watch "Naveen New topic
Author

Naveen's mock exam qn 3??

Ann , Varghese
Greenhorn

Joined: Oct 16, 2000
Posts: 13
What gets printed when the following code is compiled and run with the following arguments -
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);
}
}


Answers
a.test
b.test -1
c.0
d.1
e.2
Answer is 1 ,please explain how??
thanks
Ann
Mary Anitha
Greenhorn

Joined: Oct 13, 2000
Posts: 23
public class test {
public static void main(String args[]) {
Integer intObj=Integer.valueOf(args[args.length-1]); //args[1-1] will have 2 intobj's value will be 2
int i = intObj.intValue();//i=2
if(args.length > 1) //False
System.out.println(i);
if(args.length > 0) //True
System.out.println(i - 1); //2-1 ie 1 will be printed
else
System.out.println(i - 2);
}
}
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Mary is right. I read the question wrong at first. When you said with the following arguments java test 2, I took that as all of those as arguments. Like you would type java test java test 2 on the command line. In that case you would get
2
1
as your answer, as the first condition args.length > 1 is true. But you would still get 1 as your answer because you are taking the value of the last argument which is 2, turning into a primative, int, and then subtracting 1 from it.
I think the question should have been worded:
What gets printed when the following code is compiled and run with the following statement?
Ann , Varghese
Greenhorn

Joined: Oct 16, 2000
Posts: 13
Thank you mary & bill.
 
 
subject: Naveen's mock exam qn 3??
 
Threads others viewed
question 3 on javaprepare
Integer??
Strange output
mock exam
This is from Javaprepare.com
MyEclipse, The Clear Choice