This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings Doubt

 
gobburi saikrishna
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers

This question is from one of Mock exam

public String makinStrings() {
String s = �Fred�;
s = s + �47�;
s = s.substring(2, 5);
s = s.toUpperCase();
return s.toString();
}
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6

Answer is C, my doubt is why cant answer be E
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... If they are making the distinction I think they are, this is tricky wording that I would not expect on the real exam.

I would say that there are a total of 5 String objects here, but the 2 literals are created when the class is loaded, and this question is asking how many are created when the method is invoked.

(See Strings, Literally.)
 
Nevin kumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when class is loaded how come method local literals are created without explicitly calling the method.I disagree with weber.when class is loaded only static things are initialized(like static variables & blocks)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic