• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi-dimension array declaration and instantiation  RSS feed

 
Steven Barnacle
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Whilst pre-preparing for java certification, one of the online mock exams has slightly confused me by saying my answer was incorrect for multi-dimension array 'declaration and instantiation'.

This is one of the answers i chose - which was marked as incorrect

a)
int[][] array2d = {{123}, {4,5}};

Which looks absolutely fine to me.

One of the other answers, which i agree is correct and so does the mock exam is

b)
int [][] array2d = new int[2][2];

For clarity can anyone lend me your thoughts why a) would be deemed to be wrong ?

Thanks
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly it could be that option a) does MORE than just declaration and instantiation
It ALSO does population - there are values in the array at the end of it.

So while it does declare and instantiate the array - it also populates it
The answer which only declares and instantiates would be deemed more correct.
 
Steven Barnacle
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan - i'd agree, based on my post - the mock exam question did also populate b) too - i shortened the answer down for berevity.

If the image posts here correctly - the fullness of the question should be visible below (or see attachment)

Thanks


multi-array.png
[Thumbnail for multi-array.png]
multi-d-array-question
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the exact wording of these questions is often very important.
But this question looks to have a few flaws.

First of all the syntax of declaration of an array as (0,1) is invalid.
It should use braces/curly brackets: i.e. {0,1}
This is used in options D and E in your example, and would normally make me discard those options automatically
Disregarding that however:

Option E can be discarded as it declares a two dimensional array (or array of arrays if you prefer) but only initializes it with an array of integers.
Option C can similarly be discarded as it declares a three dimensional array but only initializes two dimensions. There is also an extra comma in that making it invalid syntax.

option D is correct apart from the use of the wrong brackets in the declaration of the initial 'array' variable.
Options A & B both look like they will compile and work.

Maybe option A is 'invalid' because it hasn't declared a 'square' array? I don't see any requirement for a 'square' array in the question, just trying to find a reason (any reason) to eliminate it.
My answer to this question would be "A" and "B"

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote: . . . There is also an extra comma in that making it invalid syntax. . . .
No, a redundant trailing comma is permitted in an array initialiser.

Agree with you: the question is definitely asked incorrectly; OP can confirm that by executing the code in the questions. Actually I shall go into pedantic mode and say that question B is incorrect because it does not show an initialisation. It shows a declaration and four assignments.

OP: Please always tell us where such questions come from; then I can add this discussion to our forum where we discuss question errors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!