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 Strings Doubt 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 "Strings Doubt" Watch "Strings Doubt" New topic

Strings Doubt

gobburi saikrishna
Ranch Hand

Joined: Jan 21, 2008
Posts: 52
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

Joined: Aug 31, 2004
Posts: 11343

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.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Nevin kumar
Ranch Hand

Joined: Mar 15, 2008
Posts: 93
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)
I agree. Here's the link:
subject: Strings Doubt
jQuery in Action, 3rd edition