which code fragment is the best potion to build and print the following string?

Given the code fragment:
<code>
String name = “Spot”;
int age = 4;
String str =”My dog ” + name + ” is ” + age;
System.out.println(str);
And
StringBuilder sb = new StringBuilder();
</code>
Using StringBuilder, which code fragment is the best potion to build and print the following string
My dog Spot is 4

Given the code fragment:

String name = "Spot";
int age = 4;
String str ="My dog " + name + " is " + age;
System.out.println(str);
And
StringBuilder sb = new StringBuilder();

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



Leave a Reply 2

Your email address will not be published. Required fields are marked *


sully

sully

so d is just a longer version of a