Which code fragment, when inserted at line 9, enables the code to print true?

Given: Which code fragment, when inserted at line 9, enables the code to print true?

Given: Which code fragment, when inserted at line 9, enables the code to print true?

A.
String str2 = str1;

B.
String str2 = new string (str1);

C.
String str2 = sb1.toString();

D.
String str2 = “Duke”;



Leave a Reply 1

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


renko

renko

Answer: A

Code question:

public class Main {

public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder(“Duke”);
String str1 = sb1.toString();
//insert code here
String str2 = str1; //true
//false tring str2 = new string(str1);
//false String str2 = sb1.toString();
//false String srt2 = “Duke”;
System.out.print(str1 == str2);
}

}

Output:
run:
trueBUILD SUCCESSFUL (total time: 0 seconds)