• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

kathy sierra question

 
Vladka Mourkova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
p.start();
}

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] ) {
a3[1]=7;
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
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by New Dude:
...am 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.

-Marc
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the expression is evaluated left to right, and up until the String concatenation, + is addition.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The + operator is syntactically left-associative, no matter whether it is later determined by type analysis to represent string concatenation or addition.

Ref: JLS 15.18.1.3. (See this for further examples.)
 
Vladka Mourkova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes ofcourse! guess itz time to go out for some fresh air:=). Thank you guys.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for a minute there i thought this was a question about Kathy Sierra
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic