Text Blocks practice questions

From OCP Java SE 17 (1Z0-829) · 14 questions on this topic

Text Blocks practice questions from OCP Java SE 17 (1Z0-829). This pack has 14 questions tagged Text Blocks, drawn from its timed mock exams. 8 of them are worked through in full below — the question, every option, why each is right or wrong, and the explanation.

Worked examples for Text Blocks

  1. Question 1

    What does this print? ```java public class Main { public static void main(String[] args) { String s = """ x y"""; System.out.println(s); } } ```

    1. A. x yCorrect answer

      Correct: the two content lines are indented 14 and 12 spaces and the closing delimiter shares the last line, so the minimum indent is 12; stripping 12 from every line leaves two spaces before the first word and none before the second, on separate lines.

    2. B. x y

      Strips per line rather than by the common minimum; the more-indented line keeps its two extra spaces because stripping is uniform by the least indentation.

    3. C. x y

      Some white space is always removed here — the compiler strips the common 12-space margin — so the raw indentation does not survive intact.

    4. D. x y

      The line terminator between the two lines is essential content and survives; nothing joins the lines onto one.

    Explanation

    Incidental white space is the single smallest indentation shared across the content lines and the closing delimiter's line, and exactly that amount is removed from every line uniformly. Indentation deeper than that minimum is essential and kept, so the more-indented line retains its extra spaces while the least-indented line keeps none. The line terminator between the lines is content and remains, so the two pieces print on separate lines.

  2. Question 2

    What does this print? ```java public class Main { public static void main(String[] args) { String s = """ hi\s"""; System.out.println(s.length()); } } ```

    1. A. 3Correct answer

      Correct: the 12-space indent is incidental and stripped and the same-line closing delimiter means no trailing newline; the space escape becomes a single space, applied after white-space stripping so it survives, giving two letters plus a space — length 3.

    2. B. 2

      Ignores the space escape, which contributes a real space character to the result.

    3. C. 4

      The escape becomes one character in the result, not two; the backslash is consumed as part of the escape.

    4. D. Compilation fails: unknown escape sequence \s

      The space escape is a legal escape sequence in Java 17, valid in both string literals and text blocks.

    Explanation

    The space escape is translated into a single space character, and crucially that translation happens after incidental and trailing white space has already been stripped, so the escaped space is protected and remains in the value. The leading indentation is incidental and removed, and the attached closing delimiter adds no newline. The content is two letters followed by one preserved space, a length of three.

  3. Question 3

    Which two statements about text blocks are correct? (Choose two.)

    1. A. A text block evaluates to a java.lang.String; there is no separate text-block typeCorrect answer

      Correct: a text block is only alternative syntax for a String literal, so its type is java.lang.String and there is no distinct text-block type.

    2. B. Variables written as ${name} inside a text block are interpolated automatically

      Java 17 has no string interpolation; ${name} stays in the result as literal characters, and values must be injected with formatted() or String.format().

    3. C. String methods such as length() and formatted() can be called directly on a text blockCorrect answer

      Correct: because a text block is a String, any String method can be invoked on it directly, including formatted(), the idiomatic substitute for interpolation.

    4. D. A text block may also be delimited by three single quotes

      The delimiter is exactly three double-quote characters; three single quotes are a syntax error.

    Explanation

    A text block is not a new kind of value — it is simply another way to write a String literal, so its runtime type is java.lang.String and every String method can be called on it directly, including instance formatting. Java 17 provides no automatic interpolation, so a ${name} sequence remains as literal text rather than being substituted. The only valid delimiter is three double quotes.

  4. Question 4

    The text block below mixes uneven indentation, an \s escape and a trailing backslash, and its closing delimiter sits further left than the content. What is printed? ```java public class Main { public static void main(String[] args) { String s = """ red\s green\ blue """; System.out.println(s.lines().count() + " " + s.indexOf("blue")); } } ```

    1. A. 3 25

      Ignores the trailing backslash line-continuation, which joins 'green' to 'blue' into one line; that leaves two lines, not three, and shifts the index of 'blue'.

    2. B. 2 24Correct answer

      Correct — the closing delimiter's 12-space indent sets the incidental whitespace, \s survives as a space, and the trailing backslash joins 'green' to 'blue', giving two lines with 'blue' at index 24 (JLS 17 §3.10.6).

    3. C. 2 12

      Forgets that the closing delimiter participates in the incidental-whitespace calculation; the retained extra indentation pushes 'blue' to index 24, not 12.

    4. D. 3 12

      Combines both errors — counting three lines (ignoring the backslash joiner) and omitting the retained indentation from the index.

    Explanation

    Incidental white space is computed over the non-blank content lines AND the closing-delimiter line, so the minimum indent is the 12 spaces of the closing delimiter, not the 16 of the content — every line keeps its extra indentation (' red', ' green', ' blue'). Escapes are then processed: \s becomes a single space that survives trailing-space stripping, and the trailing backslash is a line terminator ESCAPE that joins 'green' directly to 'blue'. The result is " red \n green blue\n": two lines, with "blue" at index 24. Option '3 25' ignores the backslash joiner; '2 12' forgets that the closing delimiter participates in the indent calculation.

  5. Question 5

    What is the length of s? ```java public class Main { public static void main(String[] args) { String s = """ a\ b"""; System.out.println(s.length()); } } ```

    1. A. 2Correct answer

      After incidental white space is stripped the raw content is the first letter, a backslash, a line terminator, then the second letter; the trailing backslash is the line-continuation escape, which suppresses the newline and joins the lines into a two-character string.

    2. B. 3

      Counts the newline between the lines, but the trailing backslash suppresses that newline, so it never reaches the resulting string.

    3. C. 4

      Assumes both the backslash and the newline remain in the result; the line-continuation escape is consumed during processing, not kept.

    4. D. 5

      Assumes leading indentation survives, but the closing delimiter aligns with the content, so all leading spaces are incidental and removed.

    Explanation

    A backslash at the end of a line inside a text block is the line-continuation escape: it deletes the line terminator that would otherwise follow, splicing the two lines together. This escape is applied after incidental white space has already been stripped, so the aligned indentation contributes nothing. What remains is the two letters joined with no separator, a two-character string.

  6. Question 6

    Which is true about the opening delimiter of a text block?

    1. A. The opening and closing """ must be at the same column

      The closing delimiter's column matters only for incidental-white-space stripping; it does not have to match the opening delimiter's column.

    2. B. Content may begin on the same line as the opening """

      Content on the opening line is exactly what the grammar forbids; only optional white space and a line terminator may follow the opening delimiter.

    3. C. A text block may use single or double quotes as the delimiter

      Only three double-quote characters delimit a text block; single quotes are never a valid delimiter.

    4. D. The """ must be followed by a line terminator; content starts on the next lineCorrect answer

      Correct: the opening delimiter is three double quotes followed by optional white space and then a line terminator, so content always begins on the next line; any other character after the opening delimiter is a compile-time error.

    Explanation

    A text block's opening delimiter must be immediately followed by nothing but optional trailing white space and a line terminator, which forces the content to start on the following line and rules out any single-line form. The delimiter is always three double-quote characters, never single quotes. The closing delimiter's position affects only white-space stripping, not where the content may begin.

  7. Question 7

    The second line of this text block contains two space characters and nothing else. Every space in the result is printed as a dot and every line terminator as a pipe. What does this program print? ```java public class Main { public static void main(String[] args) { String s = """ one two """; System.out.println(s.replace(' ', '.').replace("\n", "|")); } } ```

    1. A. one||two|Correct answer

      The two-space line is blank so it is excluded from the minimum-indentation vote and then emptied by trailing-whitespace stripping; content is "one", an empty line, "two", plus a final terminator (the closing delimiter is on its own line), printed one||two|.

    2. B. ..........one||..........two|

      Assumes the two-space line joins the indentation vote and drags the minimum to 2, leaving 10 spaces on the real lines; blank lines never take part in that vote.

    3. C. one|..|two|

      Assumes the two spaces on the blank line survive as content; trailing whitespace is stripped from every line, so a line of only spaces becomes empty.

    4. D. one||two

      Assumes a text block never ends with a line terminator; the closing delimiter is on its own line, so the final content line keeps its terminator, giving a trailing pipe.

    Explanation

    Trace: the minimum indentation is computed over the *non-blank* content lines plus the closing delimiter's line. The line holding only two spaces is entirely white space, so it is a blank line and is excluded from that computation; the closing delimiter's line is never excluded. That leaves `one` (12), `two` (12) and the closing delimiter (12), so 12 characters are stripped from the start of every line. Incidental *trailing* white space is then removed from every line, which empties the two-space line completely. The content is therefore `one`, an empty line, `two`, and because the closing delimiter sits on its own line the block ends with a line terminator: "one\n\ntwo\n" — printed as `one||two|`. Why the others are wrong: `one|..|two|` assumes the two spaces on the blank line survive as content. They do not: trailing white space is stripped from every line, and on a line made only of spaces that removes everything. `..........one||..........two|` assumes the two-space line joins the minimum-indentation vote and drags the minimum down to 2, leaving 10 spaces of indentation on the real lines. Blank lines never take part in that vote. `one||two` assumes a text block never ends with a line terminator. Putting the closing delimiter on its own line makes the last content line a full line, terminator included; only a closing delimiter placed at the end of a content line suppresses it. Exam tip: the significant-indentation rule has exactly one exception in each direction — blank lines are ignored when computing the minimum, and the closing delimiter's line is counted even though it is blank. Drop the closing delimiter to column 0 and nothing is stripped at all.

  8. Question 8

    Both content lines end with two space characters in the source; the first line then has an \s escape after them. Spaces are printed as dots and line terminators as pipes. What does this program print? ```java public class Main { public static void main(String[] args) { String s = """ ab \s cd """; System.out.println(s.replace(' ', '.').replace("\n", "|")); } } ```

    1. A. ab...|cd|Correct answer

      Correct: trailing whitespace is stripped before escapes expand, so the first line's real trailing character is the s of the escape (nothing stripped) and the escape then adds a space giving three, while the second line's two real trailing spaces vanish.

    2. B. ab.|cd|

      Wrong: this treats the escape as merely appending one space after the line's own trailing spaces were stripped. Stripping happens first on the source, where the backslash-s shields those spaces, so all three survive.

    3. C. ab..|cd|

      Wrong: this treats the escape as a zero-width fence that protects the preceding spaces but contributes nothing itself. The escape is defined as the space character U+0020, so it both fences and counts, giving three spaces.

    4. D. ab...|cd..|

      Wrong: this assumes only leading whitespace is incidental and trailing spaces are kept everywhere. Trailing whitespace is always removed unless something non-blank follows it in the source line, so cd's trailing spaces go.

    Explanation

    Trace: trailing white space is stripped from every line, and it is stripped *before* escapes are interpreted. On the first line the last source character is the `s` of the `\s` escape, not a space — so that line has no trailing white space at all and nothing is removed; the two real spaces are now interior characters. Only afterwards does `\s` become a space, giving `ab` plus three spaces. On the second line the last source characters really are two spaces, so they are incidental and vanish, leaving `cd`. The value is "ab \ncd\n", printed as `ab...|cd|`. Why the others are wrong: `ab.|cd|` treats `\s` as merely appending one space to a line whose own trailing spaces were already stripped. Stripping happens first, on the source text, where the escape's `s` character is shielding those spaces. `ab...|cd..|` assumes only *leading* white space is incidental and that trailing spaces are kept everywhere. Trailing white space is always removed unless something non-blank follows it in the source line. `ab..|cd|` treats `\s` as a zero-width fence that protects the preceding spaces but contributes nothing itself. `\s` is defined as the space character \u0020; it both fences and counts. Exam tip: `\s` is not a magic "keep whitespace" marker — it is simply an escape that expands to one space, and it works only because escapes are expanded after stripping, so its backslash-s occupies the end of the source line. Same reason a trailing `\` continues the line: the stripper sees the backslash, not a space.

Practise all 14 Text Blocks questions

OCP Java SE 17 has the full set, inside timed mock exams that mirror real exam conditions — every question with a worked explanation.

Open OCP Java SE 17

Other topics in this pack