• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

kathy sierra question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes ofcourse! guess itz time to go out for some fresh air:=). Thank you guys.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for a minute there i thought this was a question about Kathy Sierra
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic