aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array declaration doubt 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 "Array declaration doubt" Watch "Array declaration doubt" New topic
Author

Array declaration doubt

Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
Hi All,

I found one question Arrays. It goes like this :

Which of these statements are legal. Select the three correct answers.


A. int arr[][] = new int[5][5];

B. int[]arr[] = new int[5][5];

C. int[] arr = new int[5][];

D. int[] arr = new int[][5];

ANS : A, B, c

I think 'C' is incorrect way of declaring array. Can anyone confirm me that? I tried compiling it and gave me a compilation error on jdk 1.6 . Is it a valid way on jdk 1.4 and prior versions?

Link to the question

http://www.simulationexams.com/SampleQuestions/java-certification/scjp/scjp_q1.htm

Thanks in advance.

- Vivian
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You are correct: C is wrong. It declares the variable "arr" as an array of ints, and then tries to assign an array of arrays to it, resulting in a compiler error of "incompatible types."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
Thanks marc ! It boost my confidence
Deepa More
Ranch Hand

Joined: May 22, 2006
Posts: 68
Can you please tell me the difference between options C and D ?

Thanks
Deepa
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Marc,

Option D also seems to be wrong right. It does not specify the size of rows but just columns. How about that?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1511
Muthu!
C & D can be discarded on incompatible type itself. On left hand side there is single dimensional array while on right hand side it is two dimensional.

Deepa!
You need not worry about the difference as the declarations are wrong. However if you were to worrry for if code was

int [] [] arr

on left hand side.
D would still fail as per muthu's reply. i.e. in case of multi-dimensional arrays you need to specify all sizes (with or without the one in last bracket.) You can do away with last bracket but not prior to that.
[ July 02, 2007: Message edited by: Akhilesh Trivedi ]

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thats fine Akilesh. Thank you.

Deepa,

When constructing the arrays of more than 1D, you can omit the column (from the rightmost) but not the rows, as thats the only way to tell the compiler about the size of the array.

HtH.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array declaration doubt
 
Similar Threads
K&B SCJP 5 Study Guide p628 problem 9
Problem with arrays
Wrong question of Array declaration
A doubt
how to use array in array?