Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Dan test 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 "Question from Dan test" Watch "Question from Dan test" New topic
Author

Question from Dan test

Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Here is the question
class A {}
class B {
public static void main(String[] arg) {
A[] a1 = new A[1]; // 1
A[][] a2 = new A[2][1]; // 2
A[][][] a3 = new A[3][3][3]; // 3
System.out.print(a3[2][2][2]); // 4
a1[0] = new A(); // 5
a2[0] = a2[1] = a1; // 6
a3[0] = a3[1] = a3[2] = a2; // 7
System.out.print(a3[2][2][2]); // 8
}
}
What is the result of attempting to compile and run the above program?
a. Prints: null
b. Prints: nullnull
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Compile-time error at 6.
i. Compile-time error at 7.
j. Compile-time error at 8.
k. Run-time Exception
l. None of the Above

The answer is K , Run-time exception , I thought it is L.
Dan explained the answer but i didn't follow it quite well.
Could someone explain clearly.
Archana
Rory French
Ranch Hand

Joined: Apr 03, 2003
Posts: 97
Hi Archana,
To cut a long(er) story short :
initially:
- the array a2 has the dimensions [2][1]
- the array a3 has the dimensions [3][3][3]
but then each of the top level dimensions of the array a3 is assigned the array a2. So array a3 now has new dimensions i.e. [3][2][1]
So when the next line runs - System.out.print(a3[2][2][2]); - You'll get an ArrayIndexOutOfBoundsException because a3[2][2][2] exceeds the (new) dimensions of a3.
Hope this is helpful
[ May 07, 2003: Message edited by: Rory French ]
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Thank you Rory , I got that .
But do we see such questions in the real exam , it is taking little bit more time for me to figure out when the dimensions are 4x4 and above
Archana
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Questions like this would definitely be considered "fair game" on the exam. I doubt you'd see more than 1, if any at all, but I wouldn't ignore practicing some questions like this. Besides, I find it more likely that you'll get a question with arrays of smaller dimensions so being able to handle questions like this one will make those even easier.
Corey


SCJP Tipline, etc.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by Rory French:
Hi Archana,
To cut a long(er) story short :
initially:
- the array a2 has the dimensions [2][1]
- the array a3 has the dimensions [3][3][3]
but then each of the top level dimensions of the array a3 is assigned the array a2. So array a3 now has new dimensions i.e. [3][2][1]
So when the next line runs - System.out.print(a3[2][2][2]); - You'll get an ArrayIndexOutOfBoundsException because a3[2][2][2] exceeds the (new) dimensions of a3.
Hope this is helpful
[ May 07, 2003: Message edited by: Rory French ]

But how assigning a2 to a3 can change it's dimnsion?Assigning a2 to a3 takes out the null values ?Please explain.


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Veena Point:

But how assigning a2 to a3 can change it's dimnsion?Assigning a2 to a3 takes out the null values ?Please explain.

The arrays are declared as follows.
A[] a1 = new A[1]; // 1
A[][] a2 = new A[2][1]; // 2
A[][][] a3 = new A[3][3][3]; // 3
The dimensions of a3 are 3X3X3 so the compiler won't complain if an attempt is made to access a3[2][2][2].
At runtime, the array is physically created with the following line.
A[][][] a3 = new A[3][3][3]; // 3
The array a3 is really a single dimensional array that contains three object references where each reference refers to another array. Each of those three arrays contain two elements where each is a reference to another array. Each of those arrays contains only one element that is a null reference to an object of type A.
At runtime, a2 is assigned to a3 as follows.
a3[0] = a3[1] = a3[2] = a2;
As a result, each of the references contained in each of the three elements of array a3 now reference array a2. Since array a2 is only a 2X1 array rather than a 3X3 array there is no longer an element located at some of the places where elements of a3 previously existed. Although the compiler does not check for this sort of problem it will cause an exception to be thrown at runtime.
I doubt that object a3 even knows what happened. I assume the exception is probably thrown when the JVM gets the reference to a sub-array contained in a3 and then tries to access an element of a2 that does not exist.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question from Dan test
 
Similar Threads
Array Problem
multi-dimension array question - dan's exam
Mock Exam Array question
dan's sigle topic--arrays
Arrays doubt