• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Question -Array again!

 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Consider the code below:
arr[0] = new int[4];
arr[1] = new int[3];
arr[2] = new int[2];
arr[3] = new int[1];
for( int n = 0; n < 4; n++ )
System.out.println( /* what goes here? */ );
Which statement below, when inserted as the body of the for loop, would print the number of values in each row?
a) arr[n].length();
b) arr.size;
c) arr.size -1;
d) arr[n][size];
e) arr[n].length;
Well, I am unable to get it right...can someone help.
Thanks.
Mukti
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The answer should be e, because
.length;
The number of bytes of primitive data available to be read in the current buffer.
Not
.length();
It is for String class and others not for primitive array.
arr[0]=new int[4];
It means arr[0] holds the length of integer [4];
So arr[n=0].length;
arr[n=1].length;
arr[n=2].length;
It means the number of values ( length of primitive data).
Thanks,
Golam Newaz

 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Well, according to me neither of options work.
I hope,someone can explain.
Mukti

 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E is correct. Here is my reasoning:
A. False — length is a member variable of an array not a method, so no ().
B. False — array doesn't have a size variable
C. False — array doesn't have a size variable
D. False — array doesn't have a size variable, and even if it did you wouldn't call it inside of the brackets, that is used to reference a certain index.
E. True — array's length member variable tells you the lenght of the array.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic