• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jtips exam2.No.15-args.length?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic