wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can  you explain this with code... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can  you explain this with code..." Watch "Can  you explain this with code..." New topic
Author

Can you explain this with code...

Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
How the choices are working and what are the conditions to be followed while assigning arrays to other.

public class test{
public static void main(String [] args){

byte [][] big = new byte[7][7];
byte [][] b = new byte[2][1];
byte b3=5;
byte b2 [][][][] = new byte [2][3][1][2];

//Insert here
}
}

Which of the following can be inserted at the comment line(//Insert here) and still allow the code to compile.

1. b2[0][1] = b;
2. b[0][0] = b3;
3. b2[1][1][0] = b[0][0];
4. b2[1][2][0] = b;
5. b2[0][1][0][0] = b[0][0];
6. b2[0][1] = big;

Thanks...
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Supriya Nimakuri:
How the choices are working and what are the conditions to be followed while assigning arrays to other.

public class test{
public static void main(String [] args){

byte [][] big = new byte[7][7];
byte [][] b = new byte[2][1];
byte b3=5;
byte b2 [][][][] = new byte [2][3][1][2];

//Insert here
}
}

Which of the following can be inserted at the comment line(//Insert here) and still allow the code to compile.

1. b2[0][1] = b;
2. b[0][0] = b3;
3. b2[1][1][0] = b[0][0];
4. b2[1][2][0] = b;
5. b2[0][1][0][0] = b[0][0];
6. b2[0][1] = big;

Thanks...


You can answer this question by considering the dimensions of the arrays.

big - 2
b - 2
b3 - 0
b2 - 4
b2[][] - 2
b[][] - 0
b2[][][] - 1
b2[][][][] - 0
[ June 28, 2006: Message edited by: Keith Lynn ]
Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
Hai Keith ...I didnt get this logic...
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Hi Keith:
I tried to run this program. It's running fine for every other options except option 3. Whereas I guess it should also compile...isn't it??

b2[1][1][0] = b[0][0];

Here, in the LHS b2 needs a single dimension object(i.e only a single variable) and on the RHS the b[0][0] point to a single variable. Hence it should be compatible with each other. I am not getting why it's giving error. Please help!!!


Thanks
Swapnil


SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No, b2[][][] is a 1 D array.
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Thnaks Keith!!!


Regards
Swapnil
Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
Hai Keith or Swapnil...

Can you please explaim me the logic that u r applying to this question..

Regards
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
1. b2[0][1] = b;
2. b[0][0] = b3;
3. b2[1][1][0] = b[0][0];
4. b2[1][2][0] = b;
5. b2[0][1][0][0] = b[0][0];
6. b2[0][1] = big;

Hi Supriya, I am not too good at multi dimensional arrays but I will try to explain it... I will take it one by one:

1.b2 is a multidimensional array but here it's given as b2[0][1] hence, this will become a 2 D array(as 2 dimensions of b2 are already specified so there remains only 2). Whereas b is also a 2D array hence it will get fit into b2

2. I think this one is easy..here b[0][0] is the index position of element at (0,0) in b hence our primitive variable b3 will easily fit into it.

3. As I explained earlier b2[1][1][0] is a 3D array so remains only 1D Hence, it needs a 1D array and not a 2d. that's y b[0][0] can't fit into it.

4. Again we are trying to fit a 2D array into 1D array. Hence it"ll give error.

5.it's pretty clear.

6.here, b2 is also 2D array and big is also 2D array so b3 can be assigned to b2.

I think it helps...I tried my level best but still if u find any errors. I apologize for that.


Regards
Swapnil
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
A similar question is posted on this thread...

array -please explain the output

Regards

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
Hai Swapnil...
Thanx for explaining me...

Alos, Thans to Nassem and Keith
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Here is one way of seeing which declarations match.

 
 
subject: Can you explain this with code...
 
Similar Threads
A SCJP exam question (about array variable assign)
Array Dimensions
array
Multidimensional arrays
array -please explain the output