About Question enthuware.ocajp.i.v7.2.1191 :
Moderators: Site Manager, fjwalraven
-
- Posts: 5
- Joined: Tue Apr 05, 2016 10:25 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1191 :
For this question, I don't see the correct answer, can you please explain me which one is correct option? why?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1191 :
I see that correct answer and explanation has been given. (Screen shot attached.) Could you please confirm that you don't see this?
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Tue Nov 07, 2017 8:23 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1191 :
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];
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];
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1191 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Tue Nov 19, 2019 2:01 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1191 :
#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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1191 :
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!
I am updating the explanation to avoid this confusion.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 28 guests