• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Dan's exam: Question 4, Langauage Fundamentals, Main Method

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the question:
Question 4
class X {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}
}
}
Assume the above main method is invoked using the following
command line.
java X A B C D E F

What is the result of attempting to compile and run the program using the specified command line?
a. Prints: ABCDE
b. Prints: BCDEF
c. Prints: ABCDEF
d. Compile-time error
e. Run-time exception
f. None of the Above
-------------------------------------------------
I am confused why the answer is e. Why is the index incremented before the array access expression is evaluated?
Please help. Thanks.
mansi
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The run time exception is generated when trying to access the last element of the array. People overlook the result of incrementing the array index as the check is already made on array length. This is a good question.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sarma.
 
Mansi Dave
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess what I really meant to ask is why is the i incremented before the array access expression is evaluated?
Thanks.
Mansi
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a few modifications to the program so that the value of i will be printed from inside the loop expression and the array index expression. The letter A is printed with the loop expression and the letter B is printed with the array index expression. You can see that the value of i is incremented before the array index expression is evaluated. As a result, the value of i exceeds the length of the array on the last iteration of the loop.

If the above is invoked with the following command line:
java X w x y z
Then the output is:


[A,0],[B,1],x
[A,1],[B,2],y
[A,2],[B,3],z
[A,3],[B,4],


The above is followed by an ArrayIndexOutOfBoundsException.
[ January 21, 2003: Message edited by: Dan Chisholm ]
 
Mansi Dave
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
Thanks you for providing this example. It helped clear things up.
Also, thank you for taking the time to create these mock exams. They're a great study aid.
Mansi
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


class X {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}
}
}


Here is the explanation:
Inside the while loop, the conditional part is i++ < args.length. i.e
i=i+1(but this is a postfix operator).So for the first execution of while loop, the value of i=0 but the value becomes 1 after that conditional part. Like this, whenever i value reaches to 5, after post fix operation, the value becomes 6 inside the while loop. if u try to get the value of args[6],compiler will throws ArrayIndexOutOfBoundException (Subclass of RuntimeException).
If you use while(++i<args.length), then the output will be BCDEF.
----------------
Nayan
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic