File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes accessing 2 dimensional array... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "accessing 2 dimensional array..." Watch "accessing 2 dimensional array..." New topic
Author

accessing 2 dimensional array...

Ekta Matani
Greenhorn

Joined: May 17, 2005
Posts: 14
Hi,
can anybody please tell me what will be answer to the question given below,
[select 2 correct answers]

Question is:
Which code fragments will succeed in initializing a two-dimensional array named tab with a size that will cause the expression tab[3][2] to access a valid element?
1]
int [][]tab{
{0,0,0},
{0,0,0}
};

2]
int tab[][]=new int[4][];

3]
int tab[3][2];

4]
int [] tab[]={{0,0,0},{0,0,0},{0,0,0},{0,0,0}};

what i feel is,
option 4 is the correct ,since on accessing tab[3][2] it will return 0 which is valid.

option 1 is incorrect ,since size of the array tab is of 2*3 ,so on accessing tab[3][2] it will throw "array index out of bounds exception".

option 2 creates & initialises the int array tab with default value(is it correct?) which is 0.
but here on accessing tab[3][2] it throws null pointer exception.

option 3 is the only array tab declarations.

so i think option 4 is the correct ,i am confused which is the second correct answer?

Thank u,
Ekta
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
Hello "Ekta M"-

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it - please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.


Ping & DNS - my free Android networking tools app
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Ekta-

I experimented with the code a little bit and this is what I came up with. I've changed the names of the arrays just a bit so they could all reside in the same method.

The first one does not compile. It's missing an equals sign.


If you add the equals sign in it will compile, but trying to access element [3][2] gives an ArrayIndexOutOfBoundsException like you thought it would.




The second one instantiates an array of four null arrays as far as I can tell. It gives a NullPointerException.



The third one doesn't compile. The 3 and 2 need to be on the right side of the equals. I don't think it makes sense to specify the size of the array until you are actually instantiating it.


If you you change it to be the following you'll get an ArrayIndexOutOfBoundsException because arrays are zero indexed. For [3][2] to be valid you'd have to have an array that was instantiated with at least [4][3].



The last one is fine. It outputs 0. So option 4 is correct like you suspected.


Hope that helps,
Josh


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
cathymala louis
Ranch Hand

Joined: Nov 02, 2005
Posts: 77
Hi

I think 2 and 4 both are correct.


2]
int tab[][]=new int[4][];

The second expression, is a legal declaration. This expression will create 4 array objects. And the elements are set with the default values.
Because what matter where you declare an array. The array elements are initialized with the default values.

Please correct me if i am wrong.
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
I think Joshua has given it rite, still u face a problem run the below



Parallax - Change in observational position that provides a new line of sight.
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
The question looks like a somewhat garbled version of one found in Mughal's SCJP book. The mock exam on the CD with the book garbled one of the choices, so I'm guessing you ran into the same problem. Since it's copyrighted information (not a lawyer, but playing safe), I'll try answering without giving it all away.

The first choice you have is supposed to have an assignment operator between the declaration and the initialization. Incorrect.

The second choice (which was garbled for me) has a for loop which creates an array of 3 ints on each of the 4 rows. Thus, this is one of the correct answers.

The third choice (which doesn't seem to have appeared for you) declared a two-dimensional array, but initialized it as if it was a one-dimensional array. Incorrect.

The fourth choice is what you labeled as the third choice. Incorrect.

Fifth choice is what you labeled as the fourth. This is also correct.

My apologies if I shouldn't be posting this information. If so, please tell me so I may delete it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing 2 dimensional array...