Multiple choice technology programming languages

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?

  1. 1

  2. 2

  3. 3

  4. 5

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Five String objects are created: the literal "Fred" (1), "47" (2), concatenation "Fred47" (3), substring "ed4" (4), and uppercase "ED4" (5). The toString() call on line 16 doesn't create a new String since it just returns the object itself.