• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question from Dan test

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic