Win a copy of Terraform in Action this week in the Cloud 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

declaring an array of integers ?

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

What would be the answer for the following question if we compile it in jdk1.5?


Which of the following correctly declare a variable which can hold an array of 10 integers?


Select 2 correct options
a int[ ] iA
b int[10] iA
c int iA[ ]
d Object[ ] iA
e Object[10] iA

it asks to select 2 options since this question is form a mock for 1.4, but if we look at it as the compiler of jdk1.5 would see it then i think there can be more correct options.

besides a and c, i think d can also work.

Thanks
Sandy
[ September 20, 2005: Message edited by: Sandeep Chhabra ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't think option 'd' is correct, because we want to store array of integers, since integer is a primitive value not wrapper. This may work...

Integer[] iA;

Regards,
Ganesh..
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh ,

What about autoboxing? wont the value for eg 10 will autobox in the code;



This compiles and prints 10 when run.
Sandy
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion this question is rather sloppy because:

Options b and d are wrong because they are invalid syntax.

Options a and c are equivalent ways of declaring a reference to an
int array. They cannot be used to store the 10 integer primitives until they have been initialized - by assigning them the object returned from new int[10].

Option d is a reference to an array of references to Objects. Once again it has to be initialized - by assigning it the object returned from new Object[10]. You can only put references to Objects in this array, not primitives. However, in Java 5.0 autoboxing will turn int primitives into Objects by wrapping them within Integers.
[ September 20, 2005: Message edited by: Barry Gaunt ]
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry what would you finally suggest the answer should be?

By the way the question says that
Which of the following correctly declare a variable which can hold an array of 10 integers?
Does this thing matters?

Sandy
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, none. The question is badly phrased. Once again, to my mind (others may think differently):

A declaration of an array is something like:
int ai[]; // declare ai to be a reference to an array of ints

A definition of an array is something like:
int ai[] = new int[10]; // the space for 10 ints is allocated.


This problem is an example which shows us why it takes a lot of hard work to come up with a good clear, unambiguous, and correct mock exam question.

[ September 20, 2005: Message edited by: Barry Gaunt ]
[ September 20, 2005: Message edited by: Barry Gaunt ]
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Anyways Barry,
By the way the question was from JQPlus mock exam,

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

Originally posted by Sandeep Chhabra:
Hi Ganesh ,

What about autoboxing? wont the value for eg 10 will autobox in the code;



This compiles and prints 10 when run.
Sandy



In this case, the array does not hold integers. It holds Integers. I assume that since "integers" is not capitalized, they really mean "ints".

Layne
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic