File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q from javaBeat on String

 
Nevin kumar
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
11. public String makinStrings() {
12. String s = �Fred�;
13. s = s + �47�;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6

My ans is E but given ans is C,solicit ranchers put some light on it.
 
sridhar row
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this same question
[ April 19, 2008: Message edited by: sridhar row ]
 
Jart Bo
Ranch Hand
Posts: 144
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 5 String objects created here. The line

return s.toString();

will only return the String itself and not create a new one:
From the API:

toString
public String toString()This object (which is already a string!) is itself returned.

Specified by:
toString in interface CharSequence
Overrides:
toString in class Object
Returns:
the string itself.

 
Sun Craven
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is D.In my opinion,"String s" is not a String Object,here ,s is just a reference to A String.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic