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

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.938 :

Post by ETS User »

Hi,
I dont understand why the answer is number E.

wouldnt must be... ia[0].length + ", " + ia[0][0].length + ", " + ia[0][0][0].length ?
|| || ||
4 4 3

Thanks in advance.

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

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

Post by admin »

Because while initializing the second dimension, it assigns 3 to i first before creating that dimension. So ia[0][0].length is 3 instead of 4.

HTH,
Paul
If you like our products and services, please help us by posting your review here.

Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

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

Post by Daniel Clinton »

Hello, I get the question and solution but
I don't quite follow the note at the end:

"Note that if evaluation of a dimension expression completes abruptly, no part of any dimension expression to its right will appear to have been evaluated."

Can't figure this bit out...
Can you tell me what an expression evaluation completing abruptly is?

Thanks v much
Danny
:)

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

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

Post by admin »

completing abruptly means there was an error or exception while evaluating the expression. For example, let's say you have a line of code like this:
int value = arrayVariable[getIndex()];
Now, getIndex() method was supposed to return an integer value but instead of that it throws an exception. In that case, the array access evaluation will not succeed. In other words, it will complete abruptly.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

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

Post by Daniel Clinton »

Thanks Paul, I get it now
Cheers

Patrick75
Posts: 4
Joined: Sun Apr 12, 2015 10:34 am
Contact:

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

Post by Patrick75 »

I do not get this.

Given the following code (v8.2.938):
class Test {
public static void main(String[] args) {
int i = 4;
int ia[][][] = new int;
System.out.println(ia.length + ", " + ia[0].length + ia[0][0].length);
}
}

I would have thougth that 3, 4, 3 would have been printed instead of the valid answer: "4, 3, 3".
The variable ia has 3 arrays, so why is ia.length not 3?

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

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

Post by admin »

Why do you think ia has three arrays?
If you like our products and services, please help us by posting your review here.

Patrick75
Posts: 4
Joined: Sun Apr 12, 2015 10:34 am
Contact:

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

Post by Patrick75 »

Because of this line:
int ia[][][] = new int;

I see the [] 3 times.

*edit*
I thought the array lengths would be:
ia.length: 3
ia[0].length: 4
ia[0][0].length: 3
ia[0][0][0].length: 3

I think I see my mistake now, finally ;)
ia[0][0][0] does not exists and ia is the first array of course, there is only ia, ia[0] and ia[0][0].

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

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

Post by admin »

Good. Another way to think about it is - int[] ia = { 1, 2, 3 }; There is only 1 set of [] in this. Does this mean ia.length is 1 or 3?
If you like our products and services, please help us by posting your review here.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Hi,

I don't understand.


class Test{
public static void main(String[] args){
int i = 4;
int ia[][][] = new int[i = 3];
System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
}
}
Why does System.out.println return 4 for ia.length?
(it looks to me like it should be 3, because
ia has [ ][ ][ ].)

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

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

Post by admin »

You need to understand that length and dimensions are two different things. Sq Brackets denote dimensions while the number inside the bracket is the length of that dimension. I would suggest you to go through any basic tutorial on arrays before attempting these questions.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Ok, thank you.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

Does [] have the lowest operator precedence?
And () the highest?

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

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

Post by admin »

If you like our products and services, please help us by posting your review here.

daveyj
Posts: 2
Joined: Wed Oct 14, 2020 1:10 pm
Contact:

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

Post by daveyj »

I have gone through this question enthuware.ocajp.i.v8.2.938 and the comments, and I still couldn't get why ia.length is 4, after looking through books and online I'm just not getting it so I decided to write a program to display everything about a multi-dimensional array and it seems to only back up what I thought. Obviously your questions is correct, so can someone please tell me what I am missing? In my program the result of ia.length is 3 yet the only thing that has 3 is the declaration [][][]

class Test{
public static void main(String[] args){
String ia[][][] = { // ia has length 3 as it has 3 multi-dimensional arrays
{ // array ia[0] length 2, it has 2 arrays
{"1a","2a","3a","4a","5a","6a","7a"}, // array [0][0] length is 7, it has 7 elements
{"1b","2b","3b","4b","5b","6b","7b"} // array [0][1] length is 7, it has 7 elements
},
{ // array ia[1] length 4, it has 4 arrays
{"1c","2c","3c","4c","5c"}, // array [1][0] length is 5, it has 5 elements
{"1d","2d","3d","4d","5d"}, // array [1][1] length is 5, it has 5 elements
{"1e","2e","3e","4e","5e"}, // array [1][2] length is 5, it has 5 elements
{"1f","2f","3f","4f","5f"} // array [1][3] length is 5, it has 5 elements
},
{ //array is[2] length 2, it has 2 arrays
{"1g","2g"}, //array [2][0] length is 2, it has 2 elements
{"1h","2h"} //array [2][1] length is 2, it has 2 elements
}
};
System.out.println("ia.length = " + ia.length);
System.out.println("ia[0].length = " + ia[0].length);
System.out.println("ia[1].length = " + ia[1].length);
System.out.println("ia[2].length = " +ia[2].length);
System.out.println("ia[0][0].length = " + ia[0][0].length+", ia[0][1].length = "+ ia[0][1].length);
System.out.println("ia[1][0].length = " + ia[1][0].length+", ia[1][1].length = "+ ia[1][1].length+", ia[1][2].length = "+ ia[1][2].length);
System.out.println("ia[2][0].length = " + ia[2][0].length+", ia[2][1].length = "+ ia[2][1].length);
System.out.println( ia[0][0][0] + ia[0][0][1] + ia[0][0][5] + ia[0][0][6]);
System.out.println( ia[0][1][0] + ia[0][1][1] + ia[0][1][5] + ia[0][1][6]);
System.out.println( ia[1][0][0] + ia[0][0][1] + ia[1][0][3] + ia[0][0][4]);
System.out.println( ia[1][1][0] + ia[1][1][1] + ia[1][1][3] + ia[1][1][4]);
System.out.println( ia[1][2][0] + ia[1][2][1] + ia[1][2][3] + ia[1][2][4]);
System.out.println( ia[1][3][0] + ia[1][3][1] + ia[1][3][3] + ia[1][3][4]);
System.out.println( ia[2][0][0] + ia[2][0][1]);
System.out.println( ia[2][1][0] + ia[2][1][1]);
}
}

Results :
ia.length = 3
ia[0].length = 2
ia[1].length = 4
ia[2].length = 2
ia[0][0].length = 7, ia[0][1].length = 7
ia[1][0].length = 5, ia[1][1].length = 5, ia[1][2].length = 5
ia[2][0].length = 2, ia[2][1].length = 2
1a2a6a7a
1b2b6b7b
1c2a4c5a
1d2d4d5d
1e2e4e5e
1f2f4f5f
1g2g
1h2h

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

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

Post by admin »

Your code is different from the one given in the question. You are missing the line var i = 4;
If you like our products and services, please help us by posting your review here.

daveyj
Posts: 2
Joined: Wed Oct 14, 2020 1:10 pm
Contact:

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

Post by daveyj »

Hi admin,

Thank you for your reply. I know mine is different, I wasn't trying to replicate it exactly, but just to map out all the elements. What I don't understand is in my code ia.length is 3 which to my understanding is due to the face that it is a 3 dimensional array and the 3 arrays within it are of different lengths, 2, 4 & 2 as below, which is confirmed by the results of running my code.

Results :
ia.length = 3
ia[0].length = 2
ia[1].length = 4
ia[2].length = 2

But then the question is throwing me because I was expecting ia.length to be 3, regardless of what the value of i is equal to, my thinking is that ia.length should always be 3 because there are 3 [][][] and that it doesn't relate to the value of i. Obviously this is not the case but that seems to be what my code is showing, and I can't understand why.

Any help with understanding this would be greatly appreciated, my exam is scheduled for next week and I want to get my head around this before then.

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

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

Post by admin »

Dimensions is not the same as number of elements! Just because it is a three dimensional array doesn't mean its length is three. By that logic, do you think a two dimensional array will have only two elements and a one dimensional array will have only 1 element?
int[] a = {1, 2, 3, 4}. Here, a.length is 4, not 1!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 27 guests