Given the code fragment:



public class Test {
    public static void main(String[] args) {
        StringBuilder sb1 = new StringBuilder();
        String str1 = sb1.toString();
        // insert code here
        System.out.print(str1 == str2);
    }
}

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

題解

第10行的「==」運算子的運算元的數值或是參考必須要一樣才會是true。因此這題答案是選項A。