Win a copy of Spring in Action (5th edition) 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Mock Question -Array again!  RSS feed

 
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
 
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

 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!