This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats because the length is zero and invoking the first element that is ar[0] which doesn't exist will cause IndexArrayOutofBounds Exception.

-A
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mark :
Did u try compiling
int [] i = new int[0];
-Thanks
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic