wood burning stoves 2.0*
The moose likes Mock Exam Errata and the fly likes jtips exam2.No.15-args.length? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "jtips exam2.No.15-args.length?" Watch "jtips exam2.No.15-args.length?" New topic
Author

jtips exam2.No.15-args.length?

jordan gong
Ranch Hand

Joined: Jun 12, 2001
Posts: 37
Help please, what is the "args[]" here in the loop?
The answer is 5. Why?

15.Given
public class Test
{
static int i,j,k=3;
Test() {
++i;
j++;
++k;
}
public static void main(String[] args)
{
Test t1 = new Test();
String[] s1 = new String[] {"Java", "JavaScript"};
String[] s2 = new String[] {"JSP", "Servlets"};
String[] s3 = new String[] {"ASP", "SAP"};

s1=s2;
s2=s3;

for(i= --i, j=j, k=k; k < args.length ; i++, ++j, k++)
{
System.out.println(s1[i] + "," + s2[j] + "," + s3[k] );
}
System.out.println(args[i] + � � + args[j]);
}
}
What is the result of typing � java Test Java JSP ASP� in the command line?
1.Compile-time error occurs.
2. Prints JSP ASP
3.Throws RuntimeException, java.lang.ArrayIndexOutOfBoundsException.
4.Prints Java JSP ASP
5.Prints Java JSP
6. None of the above.

Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
hi,
actually for loop is not executed even once becoz condition is never met since k is 3 and k is not less than args.length but it is definitely decrementing i from 1 to 0. args.length is 3 so it comes out from for loop and simply prints args(i) and args(j) which is args(0) and args(1) respectively.
So the ans printed on the terminal is Java JSP
I hope it is clear
Jyotsna

[This message has been edited by Jyotsna Umesh (edited June 16, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi jordan,
The "args[]" is a String array that holds the command line arguments "Java" "JSP" "ASP".
When you start a Java application from the command-line ie DOS prompt, any text after the name of the class are considered arguments to the main method and are stored in the String array parameter of main. By convention the String array is named "args"
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
jordan gong
Ranch Hand

Joined: Jun 12, 2001
Posts: 37
Thanks a lot! It is easier than I thought.
So this question has nothing to do with the three Strings declared in the block, right?
jordan
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Jordan,
No, they don't come into play because the for-loop is never entered: k=4 and args.length=3. Only the initializer is executed, reducing i to 0.
args[0]=Java
args[1]=JSP
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jtips exam2.No.15-args.length?
 
Similar Threads
difference between string equals.
Technical Interview Questions
Question on Strings and String pool
== with Strings is giving a different result
immutable?????????