This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!