Java Text Blocks and String Equality

Answer Correct answer: D — true:false:false:false

Given the code fragment: What is the result? - image

  1. true:true:false:false
  2. true:false:true:false
  3. true:false:true:true
  4. true:false:false:false Correct Answer

Community Votes

D
100%

100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

The common trap is assuming text blocks are trimmed like standard strings; they actually include a newline at the end if content follows the closing quotes on a new line.

This question tests the behavior of Java text blocks regarding implicit newlines and string equality. The correct answer is D because text blocks preserve trailing newlines unless explicitly removed, causing mismatches with standard strings.

Many learners choose C, incorrectly assuming that text blocks are automatically trimmed or that interning affects the structural comparison of the block's content.

Community Discussion (7 comments)

xplorerpj 👍 1 Selected: D
D is correct answer
c6437d5 👍 2 Selected: D
D tested correct
Bharadwaj240796 👍 1
Option D. true:false:false:false is correct!!
testostaws 👍 1
Prints true:false:false:false The first comparison is true because myStr and myTextBlk1 are equal (text block doesn't include trailing new lines if there is no content after the last quote). The second and third comparisons are false because myTextBlk2 includes a newline character at the end, making it different from myStr and myTextBlk1. The last comparison is false because, despite interning, myTextBlk1 and myTextBlk2 are not equal due to the newline character in myTextBlk2.
supersquax 👍 2
right option is D. copy paste into https://editor.javadevjournal.com/java-17-compiler.html and see for yourself: public class Main{ public static void main(String[] args){ String myStr = "Hello Java 17"; String myTextBlk1 =""" Hello Java 17"""; String myTextBlk2 =""" Hello Java 17 """; System.out.print(myStr.equals(myTextBlk1)+":"); System.out.print(myStr.equals(myTextBlk2)+":"); System.out.print(myTextBlk1.equals(myTextBlk2)+":"); System.out.println(myTextBlk1.intern() == myTextBlk2.intern()); } }
meltedolive 👍 2
right option is D.
Tojose 👍 1
I think the right option is C. true:false:true:true

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The code compares a standard string myStr with two text blocks. myTextBlk1 has its closing delimiter on the same line as the content, so it does not add an implicit newline, making it equal to myStr. myTextBlk2 has the closing delimiter on a new line, which adds an implicit newline character to the content, making it unequal to myStr. The final comparison involves String.intern, but since the objects are already distinct in value, interning does not make them equal if their values differ. Thus, the output is true:false:false:false.

Why the Other Options Are Wrong

Option A assumes all comparisons are true, ignoring the newline difference. Option B assumes the second comparison is false but the third is true, failing to recognize that myTextBlk2's value differs from myTextBlk1. Option C incorrectly assumes that intern forces equality or that text blocks are always trimmed.

Community Comment Notes

Most users correctly identified D by testing the code. As one user noted, "The first comparison is true because myStr and myTextBlk1 are equal... The second and third comparisons are false because myTextBlk2 includes a newline character." Another user confirmed, "D tested correct" after running the snippet.

Exam Strategy

When working with text blocks, always check the position of the closing triple quotes. If they are on a new line, a newline character is included in the string value.

Frequently Asked Questions

Why does myTextBlk1 equal myStr but myTextBlk2 does not?

myTextBlk1's closing quotes are on the same line as the text, so no newline is added. myTextBlk2's closing quotes are on a new line, adding an implicit newline.

Does String.intern() make different text blocks equal?

No. intern() ensures that strings with identical values share the same reference, but it does not change the value of the string itself.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide