• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Fundamentals Qn

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]);
}
}


a.)The program will throw an ArrayIndexOutOfBounds exception.
b.)The program will print "java test"
c.)The program will print "java happens";
d.)The program will print "test happens"
e.)The program will print "lets happens"

Can anyone explain me the answer for this?

Thanks in advance
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you familiar with what args represents in a main method? Yes, it's a String array. But do you know how large it is, and what's in it?
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The array of command line arguments passed to main() doesnot include the name of the program. Thus the first entry in the array is "lets" and the last entry is "happens".

Aside, java naming conventions say that class names should start with an uppercase letter and should be in CamelCase.
 
pradeepa venigalla
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is given as e. (but the explanation is not given)

ok what would be the answer if we change the code for the same qn?

public class test {
public static void main(String args[]) {
System.out.println(args[0]+" "+args[args.length]);
}
}
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, it would be a)

Remember that array indexes run from 0 to dimension-1. It means that in this particular case, the line:



would try to access the 5th element in the array, which does not exist. So an ArrayIndexOutOfBounds exception would be thrown at runtime.
 
pradeepa venigalla
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys..,

Now I understood the concept....
 
reply
    Bookmark Topic Watch Topic
  • New Topic