This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

array question  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one can solve this question please...

What gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the one correct answer.
public class test {
public static void main(String args[]) {
System.out.println(args[0]+" "+args[args.length-1]);
}
}

1.The program will throw an ArrayIndexOutOfBounds exception.
2.The program will print "java test"
3.The program will print "java happens";
4.The program will print "test happens"
5.The program will print "lets happens"

Thanks in advance........
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try compiling it and running it yourself? Is there something in the output that is especially confusing?
Corey
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String array in the main() stores arguments passed on command line.It doesn't include "java" & "name of the program".
The no.of parameters,after the name of the program,gives u the array length .
Hope this helps,
args[0] = lets
args[args.length-1] = arr[4-1] = args[3] = happens.
[ May 16, 2002: Message edited by: geetha nagarajan ]
[ May 16, 2002: Message edited by: geetha nagarajan ]
 
Archana Agrawal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lott Geetha, for your simple and valuable explaination.....
archana.
 
Archana Agrawal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lott Geetha, for your simple and valuable explaination.....
archana.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Archana.
Don't know if you noticed, but you are posting your msgs twice. Can you pls. address that...
thx
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Answer is quite Simple the fifth option is correct
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!