File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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.
I agree. Here's the link:
subject: Q from javaBeat on String
It's not a secret anymore!