Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1191 :

Posted: Fri Apr 08, 2016 11:28 am
by kirankumar
For this question, I don't see the correct answer, can you please explain me which one is correct option? why?

Re: About Question enthuware.ocajp.i.v7.2.1191 :

Posted: Fri Apr 08, 2016 8:53 pm
by admin
I see that correct answer and explanation has been given. (Screen shot attached.) Could you please confirm that you don't see this?
test.png
test.png (27.43 KiB) Viewed 4195 times

Re: About Question enthuware.ocajp.i.v7.2.1191 :

Posted: Fri Jul 20, 2018 2:14 pm
by SeoaneR
re: char cA[][] = new char[3][];

I thought that both the column and row have to be initialized with their sizes otherwise a NullpointerException is thrown.
Im i right in assuming that the below code creates the rows and therefore does not throw the NullPointer.
for (int i=0; i<cA.length; i++) cA = new char[4];

Re: About Question enthuware.ocajp.i.v7.2.1191 :

Posted: Fri Jul 20, 2018 9:29 pm
by admin
new Char[3][] create an array with three elements where each element is automatically initialized to null. Because the type of the elements is char[], which in a reference type and not a primitive type.

new char[4] creates a char array with 4 elements where value of each element is automatically set to 0 (because char is a numeric primitive).

Therefore, for(int i=0; i<cA.length; i++) cA = new char[4]; assigns a char array of length 4 to each of the elements of cA.

Now, which dimension you call rows and which columns is up to you. Java arrays don't have the concept of rows and columns.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1191 :

Posted: Sat Dec 07, 2019 7:18 pm
by tcroots19
#5 in the answer says "creates a one dimensional array", but it doesn't actually do that right? this fails to compile, the right side is a one dimensional array...that tries to go into a two dimensional array...let alone that it's not char. Am I right?

Re: About Question enthuware.ocajp.i.v7.2.1191 :

Posted: Sat Dec 07, 2019 11:31 pm
by admin
The explanation is in the context of the problem statement, which asks for a two-dimensional array of chars. Variable declaration of the options is char[][], which is correct but the instantiation part of Option 5 is { "1234", "1234", "1234" };. The explanation is about this part.It creates a one dimensional array and that too of Strings. The implied (unsaid) part is, that is why it cannot be assigned to cA, which requires a two dimensional array of chars.

I am updating the explanation to avoid this confusion.

thank you for your feedback!