Given the code fragment: Using StringBuilder, which code fragment is the best potion to
build and print the following string My dog Spot is 4
A.
sb.append(“My dog ” + name + ” is ” + age); System.out.println(sb);
B.
sb.insert(“My dog “).append( name + ” is ” + age); System.out.println(sb);
C.
sb.insert(“My dog “).insert( name ).insert(” is ” ).insert(age); System.out.println(sb);
D.
sb.append(“My dog “).append( name ).append(” is ” ).append(age);
System.out.println(sb);
A, D
Answer: D
Explanation:
Yes option A does the job but is not the best solution nor the best way to use string builder.
There is no reason to use string builder if you already have concatenated them, is a redundant operation.