File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes kathy sierra question 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 "kathy sierra question" Watch "kathy sierra question" New topic

kathy sierra question

Vladka Mourkova

Joined: Jun 06, 2006
Posts: 2
hi guys,
juz going thro self-test q's in kathy sierra, in chapter 3 therez this question :

class PassA {
public static void main(String [] args) {
Pass p = new PassA();

void start() {
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);

long [] fix(long [a3] ) {
return a3;


what is the result?
A. 12 15
B. 15 15
C. 3 4 5 3 7 5
D. 3 7 5 3 7 5
E. Compilation fails
F. An exception is thrown at runtime

Kathy says its B, since a1 and a3 refer to the same long array object.

I had this slight doubt that this answer would be right if the first print statement does not contain an " ".
Since it contains " ", shouldnt't the String Concatenation rule be applied, and it should simply print 375 15 ?

am i missing somethin in here?
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by New Dude: i missing somethin in here?

Yes, you're missing Bert Bates.

I think you also missed our JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names. Please revise your display name here.


"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
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
No, the expression is evaluated left to right, and up until the String concatenation, + is addition.
marc weber

Joined: Aug 31, 2004
Posts: 11343

The + operator is syntactically left-associative, no matter whether it is later determined by type analysis to represent string concatenation or addition.

Ref: JLS (See this for further examples.)
Vladka Mourkova

Joined: Jun 06, 2006
Posts: 2
yes ofcourse! guess itz time to go out for some fresh air:=). Thank you guys.
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

Just for a minute there i thought this was a question about Kathy Sierra

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
I agree. Here's the link:
subject: kathy sierra question
It's not a secret anymore!