• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A SCJP exam question (about array variable assign)

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.public class Test {
2.public static void main(String [] args) {
3.byte [][] big = new byte[7][7];
4.byte [][] b = new byte[2][1];
5.byte b3 = 5 ;
6.byte b2 [][][][] = new byte[2][3][1][2];
7.
8.}
9.}

which code can insert into line 7 and compile successful (choise 4)
A. b2[0][1] = b;
B. b[0][0] = b3;
C. b2[1][1][0]= b[0][0];
D. b2[1][2][0] = b;
E. b[0][1][0][0] = b[0][0];
F. b2[0][1] = big;

accord to the text book , the answer is A,B,E,F.

i do not understand why C is false? if C is false why E is true?
why D is false? if D is false, why A is true?
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi demilich,

Welcome to the ranch. We don't really have a lot of rules around here, and we really love to get new members, but I'm afraid that you're violating the few rules we do have.

We have a naming policy which states you need to have a name that I can't tell is fake. Right now, I can tell that your name is fake, so please change it to something that will confound me.

Also, please change your signature to something that doesn't include the word 'f*cking', or other, like-minded terms. While I'm personally a big fan, we get younger visitors here, and we like to keep the site work friendly.

Thanks,
Max
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm iam seeing the f word for the first time , I think some strict policies need to be placed with regards to using abuse
 
Shiao Kung Chux
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Max Habibi:
Hi demilich,

Welcome to the ranch. We don't really have a lot of rules around here, and we really love to get new members, but I'm afraid that you're violating the few rules we do have.

We have a naming policy which states you need to have a name that I can't tell is fake. Right now, I can tell that your name is fake, so please change it to something that will confound me.

Also, please change your signature to something that doesn't include the word 'f*cking', or other, like-minded terms. While I'm personally a big fan, we get younger visitors here, and we like to keep the site work friendly.

Thanks,
Max
OK, i update

 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair enough. If you'll please change your last name to something I don't recognize as obviously fake, I'll see about answering your question. Deal?

M
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!


QUESTION: why C is false?
ANSWER: b[0][0] is a int(namely, the number 5). However, b2[1][1][0] point to an array. You can't put an int into a space that's expecting an array, as b2[1][1][0] is.



QUESTION:if C is false why E is true?
ANSWER: because b[0][1][0][0] point to a position that holds a int. And b[0][0] is a int(namely, the number 5).


QUESTION: why D is false?
ANSWER: This is more subtle. b is a two dimensional array, but the position indicated by b2[1][2][0] can only hold a one dimensional array.

QUESTION:if D is false, why A is true?
ANSWER: b2 is a four dimensional array. At position b2[0][1], it can hold a two dimensional array. Since b happens to [b]be[/i] an two dimensional array, A is legal.

hth,
M
[ January 26, 2006: Message edited by: Max Habibi ]
 
Shiao Kung Chux
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i see,

complex problem.

whatever, b2[][][] can put a one dimation array,
b2[][] can pub a two dimation array,
b2 [] can pub a tree dimation array,
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiao Kung Chux wrote:1.public class Test {
2.public static void main(String [] args) {
3.byte [][] big = new byte[7][7];
4.byte [][] b   = new byte[2][1];
5.byte b3 = 5 ;
6.byte b2 [][][][] = new byte[2][3][1][2];
7.
8.}
9.}

which code can insert into line 7 and compile successful (choise 4)
A. b2[0][1] = b;
B. b[0][0] = b3;
C. b2[1][1][0]= b[0][0];
D. b2[1][2][0] = b;
E. b[0][1][0][0] = b[0][0];
F. b2[0][1] = big;

accord to the text book , the answer is A,B,E,F.

i do not understand why C is false? if C is false why E is true?
why D is false? if D is false, why A is true?



I spent a lot of time figuring out how to handle multi dimensional arrays.
If you iterate over a one dimensional array assume int a[] = new int[3];  you refer to element as a[0],a[1] and a[2]

For two dimensional array int a[][] = int[2][2]; (initializing array)  This represents a 2dimensional array. If you fix the first position.. what you receive is a one dimensional array.. which means a[0][] refers to first one dimensional array a[1][] refers to second one dimensional array since index starts from 0.   a[0][0] represents a constant value .


Similarly for 3 dimensional array int a[][][] = new int[2][3][4];  If you fix the first position for eg: a[0][][] what you receive is a two dimensional array at first index (0).. a[1][][] means you are referring to a two dimensional array at 2nd index.

Visually you can imagine a 3D array as a rubix cube.. which is nothing but a collection of 2D arrays.. When you iterate over the the 3rd dimension for every iteration you refer to a plane which is a 2D array. Which means for (i=0;i<4;i++) {
// for every iteration below this you get a 2D array from these 2 inner loops)
  for (j=0;j<4;j++) {
   for (k=0;k<4;k++) {
  }
 }
}

for i=0 you refer to first 2 dimensional array.. Similarly for i =1 you refer to 2nd two dimensional array. a[0][j][k]  a[1][j][k]

Now let us explore the answers one by one.
A)b2[0][1] = b means you have fixed first two indexes of 4 dimensional array b2[][][][] and this refers to two dimensional array and b also refers to a 2 d array. So you can assign a 2 d array to a 2d array.
b)b[0][0] = b3; b[0][0] refers to first row and first column of array and it can be any integer constant. b3 is also constant (can be assigned to another variable of same type)
c)  b2[1][1][0]= b[0][0];  LHS refers to one dimensional array since first 3 positions are fixed from b2[][][][] and b[0][0] refers to a constant byte type . You cannot assign a byte type constant to a one dimensional byte array.
d)b2[1][2][0] = b;LHS is one dimensional array because first 3 positions are finalized..b is a 2D array. SO you cannot assign a 2d array into 1D array.
e)b[0][1][0][0] = b[0][0]; LHS refers to a constant value since all four positions are finalized. RHS also refers to a constant value. Hence correct.
f)b2[0][1] = big; LHS refers to a 2D array since only first positions are fixed from b2[][][][] and big also refers to 2D array. Hence a 2D array can be assigned to a 2D array.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic