File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q from javaBeat on String " Watch "Q from javaBeat on String " New topic
Author

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:

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.



SCJP, SCWCD
Sun Craven
Greenhorn

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.
 
jQuery in Action, 2nd edition
 
subject: Q from javaBeat on String
 
Similar Threads
4 quest diffulties please help
String objects
exception question from kb
URL rewriting
GC