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

Given the code fragment:

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

Download Printable PDF. VALID exam to help you PASS.

4 thoughts on “Which code fragment, when inserted at line 9, enables the code to print true?

  1. A
    public static void main(String[] args) {
    StringBuilder sb = new StringBuilder(“Duke”);
    String str1 = sb.toString();
    String str2 = str1;
    System.out.println(str1==str2);
    }

  2. Answer A:

    The str1 has a string value, but it is not literal. Therefore, this “Duke” will not be pooled in the string pool. Accordingly, the (==) operator will examine reference equality. So only option A is correct.

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.