This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array and Polymorphism questions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array and Polymorphism questions." Watch "Array and Polymorphism questions." New topic
Author

Array and Polymorphism questions.

Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
Hi,
I am working my way through some SCJP questions and I have come across two that I don't understand.
First one if from ExamLab.

Which is valid:

Ok, I know why A&B are false, but the syntax for C&D&E is completely new to me. C is the only one that is true, but I have no idea why.

- second question removed until the source is cited -
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

I'll move this thread to our SCJP forum, but not before answering your first question.

Eoin Brosnan wrote:Hi,
I am working my way through some SCJP questions and I have come across two that I don't understand.
First one if from ExamLab.

Which is valid:

Ok, I know why A&B are false, but the syntax for C&D&E is completely new to me. C is the only one that is true, but I have no idea why.

A is invalid because you either create an array with a size (new int[0]) or with its values ({}), not both.

B is invalid because it's impossible to only create an array without doing anything with it. I was actually a bit surprised by this, because it is possible to call "new Object();". Apparently the compiler is smart enough to see that an array without a reference is useless, whereas an object can still do things in its constructor.

C uses two "techniques". The first is create an array while immediately assigning its values: new int[][]{{1}}. Then its first element is accessed using [0]; the result is an int[] since the array itself is an int[][].

D tries to do the same, but the dimensions are wrong. new int[]{0} creates an array with one element with value 0. The first [0] returns that element, an int. The second [0] is invalid, since this is only allowed for arrays, not for ints.

E is just wrong.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
Rob, thanks for the explanation. However isn't option C similar to B in that it creates an array but doesn't assign it to anything?
For example:


Thanks again.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

In example C, you still use the array. You don't assign it to a variable, but you access it nonetheless. And that's the difference with example B, where you create an array and ignore it immediately.
Arun Arun Kumar
Greenhorn

Joined: Sep 03, 2011
Posts: 4
what is the difference between

int[][] array1 = new int[][] {{1,2,3}, {4,5,6}};
int[][] array2 = {{1,2,3}, {4,5,6}};
gihan dissanayaka
Greenhorn

Joined: Sep 03, 2011
Posts: 8
arun,i think this two array initialisations is same .only two ways of expression.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array and Polymorphism questions.
 
Similar Threads
Khalid Mock Exam Array
Khalid Mock Exam
Please hava a look at this question about array !!!
Clrify results from SCJP mock exam site
Stuck on Homework