File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jtips exam2.No.15-args.length?

 
jordan gong
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jordan gong
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic