Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array

 
arivu mathi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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];
//line to be inserted here
}
}
which of the following lines of code could be inserted at line 7, and still allow the code to
compile? (Choose four that would work.)
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. b2[0][1][0][0] = b[0][0];
F. b2[0][1] = big;
 
Chaitanya Vivek
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Draw the diagrams for each array you declared and remember 2 rules related to Array assignments. You will conclude with A,B,E,F as answers.

thanks
chaitanya
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arivu,



which of the following lines of code could be inserted at line 7, and still allow the code to
compile? (Choose four that would work.)
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. b2[0][1][0][0] = b[0][0];
F. b2[0][1] = big;

big & b are byte[][], b3 is a byte and b2 is byte[][][][].

A. b2[0][1] and b are both byte[][], GOOD!
B. b[0][0] and b3 are both byte, GOOD!
C. b2[1][1][0] is byte[] and b[0][0] is byte, BAD!
D. b2[1][2][0] is byte[] and b is byte[][], BAD!
E. b2[0][1][0][0] and b[0][0] are both byte, GOOD!
F. b2[0][1] and big are both byte[][], GOOD!
[ July 13, 2006: Message edited by: Douglas Chorpita ]
 
Asha Pathik
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arivu,



I use a very simple method to solve questions like this. For any assignment just check whether the no. of dimensions on the R.H.S are same as the no. of dimensions required on the L.H.S.

e.g
A. b2[0][1] = b;

Array 'b2' has 4 dimensions. In the assignment you can see only 2 dimensions on the L.H.S that means whatever is on the R.H.S should have exactly 2 dimensions, neither more than 2 nor less than 2.

Since array 'b' has been declared with exactly 2 dimensions, this assignment is perfectly valid.

The only thing you must take into account while doing assignments are the "NullPointerException" and the "ArrayIndexOutOfBoundsException".

So, can you do the rest all by yourself.
B. b[0][0] = b3;
C. b2[1][1][0] = b[0][0];
D. b2[1][2][0] = b;
E. b2[0][1][0][0] = b[0][0];
F. b2[0][1] = big;

Hope this helps
 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how the answer E is correct?
 
Asha Pathik
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suhita,

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

If you look carefully in this we are actually accessing the value stored at b[0][0]( a byte value) and saving the same in b2[0][1][0][0] which is perfectly legal.

hth,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic