About Question enthuware.ocajp.i.v7.2 . 1191 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2 . 1191 :

Post by ETS User »

I didn't understand this one. Can anyone help me figure out what happened?

Thanks

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

What is it that you are not able to understand? If you give some details, we can help you out :)

Guest

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

Post by Guest »

admin wrote:What is it that you are not able to understand? If you give some details, we can help you out :)
Thanks for quick reply.
char cA[][] = new char[3][];
for (int i=0; i<cA.length; i++) cA = new char[4];
This question says that this array refers to a valid element cA[3][2], but it didn't create the second dimensio of the array. My question is: when I do such a thing, can I access any range of elements in the second brackets?

Thanks

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It is creating both the dimensions. It creates the first dimension, when it does new char[3][]; Since the first dimension is an array of arrays, all the elements are initialized to null.

Now, for each element in this array, it assigns: cA = new char[4]; This is the second dimension. It changes the original null references to a new char arrays.

Since the second dimension an array of primitives (char is a primitive data type), all elements of this second array are already initialized to 0.

HTH,
Paul.

Matheus

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

Post by Matheus »

I think that this question was modified, because now the question asks to select an option that create an array of size cA[2][3] and says that this one is the correct answer:
char cA[][] = new char[3][];
for (int i=0; i<cA.length; i++) cA = new char[4];

isn't this one:
char[][] cA = { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };
AND
char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char[ ]{ 'a', 'b', 'c' } };
??

Thanks

Matheus

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

Post by Matheus »

Sorry, I misunderstood this question

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

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

Post by baptize »

Matheus wrote:Sorry, I misunderstood this question
exactly what i did :x
Hopefully i would pay more attention in the exam....

rodrigocesar
Posts: 2
Joined: Wed Feb 18, 2015 9:29 pm
Contact:

Mistake question 39 on enthuware.ocajp.i.v7.2.1191 :

Post by rodrigocesar »

Hi everyone,

I've found a mistake on what the software says it's the correct answer of this question:
Which of the following code fragments will successfully initialize a two-dimensional array of chars named cA with a size such that cA[2][3] refers to a valid element?

Code: Select all

1.   char[][] cA = {  { 'a', 'b', 'c' },  { 'a', 'b', 'c' }   }; 

2.   char cA[][] = new char[3][];   for (int i=0; i<cA.length; i++) cA[i] = new char[4]; 

3.   char cA[][] = { new char[ ]{ 'a', 'b', 'c' }  ,   new char[ ]{ 'a', 'b', 'c' }  }; 

4   char cA[3][2] = new char[][] {  { 'a', 'b', 'c' },   { 'a', 'b', 'c' }   }; 

5.   char[][] cA = { "1234", "1234",  "1234"  }; 
Explanation
1 and 3 declare a two dimensional array alright but they create the array of size 2, 3. And cA[2][3] means we need an array of size 3, 4 because the numbering starts from 0.
4 : You cannot put array size information on LHS.
5 : This is a one dimensional array and that too of strings. Note that a java String is not equivalent to 1 dimensional array of chars.
This leaves us with only one choice 2.
This is incorrect! The question asks which options will create a multi-dimensional array of char with a size such that cA[2][3], and options 1 and 3 already do that! If you put the codes (1 and 3) and then run on Java, typing an extra line: System.out.println(cA.length +" "+ cA[0].length), the console will display 2 3, which is the options 1 and 3.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

The answer is correct. Please read the explanation carefully.

rodrigocesar
Posts: 2
Joined: Wed Feb 18, 2015 9:29 pm
Contact:

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

Post by rodrigocesar »

I'm sorry, but I still think this question has ambiguity, since if you test on console System.out.println(cA.length +" "+ cA[0].length), it's gonna print 2 and 3, so, this answer is correct too.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

The question is NOT asking you to create an array of size 2 and 3. Please read the problem statement carefully.

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post by Sergiy Romankov »

I also believe that this question is ambiguos

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Sergiy Romankov wrote:I also believe that this question is ambiguos
And why do you think so?

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post by Sergiy Romankov »

when I write new int[2][3], I allocate array with TWO elements, which are itself arrays and stored THREE elements each. So in question create multi-array with size [2][3]. What I miss?

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

The question is NOT asking you to create an array of size 2 and 3. Please reread the question.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests