|
![]() |
Originally posted by Shivaji Marathe:
Sonir :
Look at this line carefully
int i[] = new int[0]; //1
How many elments does this array contain after this statement?
Try to do a println of i.length and see what happens?
Let me give you another hint -
int j[] = new int[2];
How many elments does the array j contain after this statement?
Now look at this statement
System.out.println(i[0]); //2
Which element of the array are you trying to refer?
Which element of array j does the following statement refer to ?
j[1]=10;
Do you now see how you can answer your own question?
Originally posted by sonir shah:
Options:
A: The code does not compile because of line //1.
B: The code compiles but throws exception at
runtime because of line //1.
C: The code does not compile because of line //2.
D: The code compiles but throws exception at
runtime because of line //2.
Answer : B) It is throwing ArrayIndexOutOfBoundsException.why??
I feel it should run fine and give an output of '0'(zero) because int will get a default value..
please explain
Rob
SCJP 1.4
Originally posted by Rob Ross:
Options:
A: The code does not compile because of line //1.
B: The code compiles but throws exception at
runtime because of line //1.
C: The code does not compile because of line //2.
D: The code compiles but throws exception at
runtime because of line //2.
Answer : B) It is throwing ArrayIndexOutOfBoundsException.why??
I feel it should run fine and give an output of '0'(zero) because int will get a default value..
please explain<hr></blockquote>
The explanations givin above by everyone are correct, but the orignal post that stated the correct answer is B is wrong!
Line 1 is fine, the problem is with line 2 where the ArrayOutOfBoundsException occurs.
The correct answer is D, NOT B.
Rob[/QB]
Rob
SCJP 1.4
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |