This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q from javaBeat on String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q from javaBeat on String " Watch "Q from javaBeat on String " New topic

Q from javaBeat on String

Nevin kumar
Ranch Hand

Joined: Mar 15, 2008
Posts: 93
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

Joined: Jan 16, 2008
Posts: 162
check this same question
[ April 19, 2008: Message edited by: sridhar row ]
Jart Bo
Ranch Hand

Joined: Oct 22, 2007
Posts: 144
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:

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

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

Sun Craven

Joined: Mar 13, 2008
Posts: 27
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.
Have you tried LearnNowOnline?
subject: Q from javaBeat on String