Ask your own question, for FREE!
Computer Science 6 Online
OpenStudy (javk):

What is the output of the following java class? Please state if you think there is an error and if so state what the error is:

OpenStudy (javk):

``` class p { public static void main(String [] args) { int [] a = new int [10]; for (int i=0; i<10; i++) a[i+1]=i; System.out.print(a[3]); } }

OpenStudy (javk):

Why is it throwing an exception? Where does the index go out of bounds?

OpenStudy (anonymous):

when i=9 in the loop a[i+1] ==> a[10] is out of bounds because arrays go from [0] to [size-1] which in this case is [0] to [9]

OpenStudy (javk):

Thanks, in a moment of ultimate inspiration, I decided to draw out the array in a table, and then I got it, who knew you could draw it out (ok, I did but I just didn't connect the two) =)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!