This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array" Watch "array" New topic
Author

array

arivu mathi
Ranch Hand

Joined: Jun 26, 2006
Posts: 38
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

Joined: May 17, 2006
Posts: 36
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

Joined: May 09, 2006
Posts: 97
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 ]

SCJP 1.4 - 95%
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
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


SCJP 1.5
Suhita Reddy
Ranch Hand

Joined: Jun 09, 2006
Posts: 60
how the answer E is correct?
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
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,
 
jQuery in Action, 2nd edition
 
subject: array
 
Similar Threads
Self Test Question on Arrays by Kathy's Book
Array Dimensions
Multidimensional arrays
Multidimensional Arrays
multi arrays