aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sierra Bates SCJP 6 Ch. 3 No. 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sierra Bates SCJP 6 Ch. 3 No. 3" Watch "Sierra Bates SCJP 6 Ch. 3 No. 3" New topic
Author

Sierra Bates SCJP 6 Ch. 3 No. 3

Tod Stroszer
Greenhorn

Joined: Oct 24, 2007
Posts: 28
Sierra Bates SCJP 6 Ch. 3 No. 3

Help me understand this question.



Obviously there is an error in the code at line 4

int[] b = (int[]) a[1];

int[] a is a 2 dimensional array and cannot be cast to the size of a one dimensional array.

But the choices given are

A. 2
B. 4
C. An exception is thrown at runtime
D. Compilation fails due to an error on line 4
E. Compilation fails due to an error on line 5
F. Compilation fails due to an error on line 6
G. Compilation fails due to an error on line 7


If you choose D you are wrong - the code compiles, but an exception is thrown at runtime - so choice C is correct.

How can we tell that the error at line 4 will result in a runtime error vs. a compilation error? On the test we will not have the option of being able to compile this code ourselves, we just have to determine the result from the code.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438


it is ok that this is two dimensional array
consider it array of arrays
so every elements in this array is itself a single dimensional array right?
now
we can assign a single dimensional array to another single dimensional array
so

here we are creating a single dimensional array and assigning a[1](which is single dimensional array to in[] b)
so this does not give compiler error
can you find the cause for the runtime Exception?

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066



There is no problem with this line. There will be a ClassCastException in some where else in your code. Could you spot it? You can't cast an array of one dimension to other dimension.

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
a[1] is an element of a two-dimensional array. Elements of a two-dimensional array are one-dimensional arrays.

so if you have



you can go:


all the elements of a, namely a[0], a[1] and a[2], will be int arrays, NOT ints.

the elements of the elements of a will be ints.

meaning a[1][0] will be 0.


Oracle Certified Professional: Java SE 6 Programmer
Oracle Certified Expert: Java EE 6 Web Component Developer
Oracle Certified Expert: Java EE 6 Enterprise JavaBeans Developer
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,



Look at these two lines, line 3 is assigning a single dimensional array to object which is legal as array is an object.
Now when you see at line 4, you are basically trying to convert a single dimensional array to double dimensional
array which is not possible, so the compilation with fail because of the error at line 4.

Hope this helps,



Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Tod Stroszer
Greenhorn

Joined: Oct 24, 2007
Posts: 28
Prithvi Sehgal wrote:Hello,



Look at these two lines, line 3 is assigning a single dimensional array to object which is legal as array is an object.
Now when you see at line 4, you are basically trying to convert a single dimensional array to double dimensional
array which is not possible, so the compilation with fail because of the error at line 4.

Hope this helps,




I interpreted the above code like this:

a is a 2 dimensional array

int[][] a = {{1,2,}, {3,4}};

so here you are changing its type to Object


Object o1 = a;

and here you are trying to cast an Object to a 2 dimensional array

int[][] a2 = (int[][]) o1;

-so trying to cast an Object to a 2 dimensional array will give you a ClassCast exception and runtime error?

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

As I told you, the problem is with the line below. Check the error message, when you compile.



You are assigning a two dimensional array into one dimensional array. Here is the problem. Check it with your compiler.
Tod Stroszer
Greenhorn

Joined: Oct 24, 2007
Posts: 28
I see it now, thank you!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Tod Stroszer wrote:I see it now, thank you!


So, did you solved it?
Tod Stroszer
Greenhorn

Joined: Oct 24, 2007
Posts: 28
Abimaran Kugathasan wrote:
Tod Stroszer wrote:I see it now, thank you!


So, did you solved it?





First o1 is an Object, then it is cast to a 2 dimensional array, then the code attempts to cast a 2 dimensional array to a 1 dimensional array - this causes the runtime exception.


Yes, if you assign an array to a previously declared array reference the array must be of the same dimensions as the array reference you're assigning it to.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

And also, have look on your original code, line number 8. Got the difference!
muthuraj sennakesavan
Greenhorn

Joined: Aug 02, 2010
Posts: 1
ya that is correct ...
int[][]b2 =(int [][])01 ..then it will not show any exception.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hey,
I guess i was too busy looking at the correct source code and didn't answer properly.

Best Regards,
 
 
subject: Sierra Bates SCJP 6 Ch. 3 No. 3
 
Similar Threads
Please explain this array question to me
not initialized
Can anybody help me!!!!!!!!!
why can't a function be called from a static context
Help on this SCJP question