Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String objects

 
Kanika Khanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should be ans ???

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
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edited to remove answer. - Stephan van Hulst]
 
Stephan van Hulst
Bartender
Pie
Posts: 5432
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Astha, please refrain from posting straight answers. We're here to help people reason for themselves.

Kanika, what do you think is the answer, and why?
 
Kanika Khanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But ans given is3??
What about toString()??? It doesn't create any new string object???
 
Kanika Khanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer should be either 5 or 6.
6 in case toString() creates a new string Object.But it was given 3 in some test papers.
 
Stephan van Hulst
Bartender
Pie
Posts: 5432
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can tell from the String.toString() documentation that it won't return a new object. Furthermore, the question is how many Strings will be created by the method, right? Do you recall when String literals are created?
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Kanika Khanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean to say there are two literals "Fred" and "47". Rest 3 are objects.bt how can we make sure that both string are present in pool???. If they are not in pool,then new objects would be created for both.right??? wrote:
 
Stephan van Hulst
Bartender
Pie
Posts: 5432
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They will be in the pool from the point the class is loaded, if I'm not mistaken. Anyway, they are not guaranteed to be created during the method call.
 
Mezan Shareef
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kanika Khanna wrote:What should be ans ???

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 answer would be 5.
1st object - "Fred"
2nd object - "47"
3rd object - "Fred47"
4th object - "d4"
5th object - "D4"

And there is only 1 reference variable to objects, if try to print output for s. It should be 5th object. D4.

correct me, if i am wrong. Thankyou guys.
 
Stephan van Hulst
Bartender
Pie
Posts: 5432
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reread this thread, Mezan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic