wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why does'nt it get a default value..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why does Watch "Why does New topic
Author

Why does'nt it get a default value..?

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

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
Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
Thats because the length is zero and invoking the first element that is ar[0] which doesn't exist will cause IndexArrayOutofBounds Exception.

-A
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
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?
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
int i[] = new int[0];
this line does not make any sense. you are trying to create an array of no elements. does this have any sense ? so should it not give a compile error ?

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?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
well, it doesnt make much sense but it's useful. say what happens when u don pass any command line arguments to the code and checking the length of the command line parameter??
if this is not supported then it will result into error (NullPointerException) which is not good.
so, when we don pass any command line arguments to the program it still creates an array of ZERO length so we dont result into NullPointerException while checking length thru args.length where args is the command line param array..
regards
maulin
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

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


Rob
SCJP 1.4
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
Rob i think the problem is line 1.
because see this:
if we made line 1 as int i[] = new int[1];
then it would all be fine. right ? there would be runtime exception at all.
so the problem is line 1 and answer B is right ?
right ?
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]
Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
Hi mark :
Did u try compiling
int [] i = new int[0];
-Thanks
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
yes. why ? infact if you see the discussion in this thread this point has been discussed, though implicitly.
Originally posted by Arsho, Ayan:
Hi mark :
Did u try compiling
int [] i = new int[0];
-Thanks
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
i thought that ans B is wrong but thats not the case. it says "runtime exception BECAUSE OF ...."
now, as we did allocated len zero to the array and tried to access it on second line improperly it gave errors.
if it were "at which line exception occurs..." then i would be sure that B is wrong.
regards
maulin.
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
B is wrong. The correct answer is D (line 2). Run this code :
********************************************
public class Test018 {
public static void main(String args[]) {
System.out.println("1");
int i[] = new int[0]; //1
System.out.println("2");
System.out.println(i[0]); //2
}
}
********************************************
Output is :
==============================================
1
2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at Test018.main(Test018.java:8)
==============================================
Jamal
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Jamal is correct (but hey, I said the same thing a few posts ago!!! )
this is a perfectly valid statement:
int i[] = new int[0];
It creates an int array named "i" of zero length.
Now, this style is BAD FORM. It should be written like:
int[] i = new int[0];
because it makes it clearer that i is an int array.
Rob
Kem Bro
Greenhorn

Joined: Oct 17, 2001
Posts: 25
Just looking at the question and the answer the original poster gave, it looks as though perhaps
they are thinking of:
int[] i = {0};
To the best of my knowledge
System.out.println(i[0]); will print '0' in this case.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does'nt it get a default value..?