Wednesday, May 8, 2024
HomeJavaQuiz your self: Java textual content blocks and escape sequences

Quiz your self: Java textual content blocks and escape sequences


Quiz Yourself, Oracle Java, Oracle Java Certification, Oracle Java Guides, Oracle Java Learning, Oracle Java Prep, Java Preparation

If a triple double quote sequence signifies the start and finish of a textual content block, how do you assign a triple double quote sequence to a string?

Which of the next are legitimate Java textual content blocks? Select three.

A. var v1 = “”””””;.

B. var v2 = “””abc”””;.

C. var v3 = “””

         “

         “””;

D. var v4 = “””

          s

         “””;

E. var v5 = “””

         “

         “””;

F. var v6 = “””

         “””

         “””;

Reply. A Java textual content block begins with triple (three) double quote marks, and that character sequence should kind the top of that line of supply code. It’s permitted to have areas or tabs earlier than the precise newline character, however nothing else is permitted.

In view of this, possibility A is inaccurate as a result of the textual content block opening delimiter (“””) has no newline character after it. As a substitute, it’s adopted instantly by one other triple double quote sequence.

You’ll be able to see that possibility B can also be incorrect as a result of there are textual content characters (abc) on that line of code along with one other triple double quote sequence.

Choice C kinds a legitimate textual content block that creates a string that accommodates a single double quote sequence adopted by a newline character. Subsequently, possibility C is right.

One of many helpful traits of Java’s textual content block function is that it removes the particular which means of many characters, notably the double quote mark sequence. Due to this, it’s attainable to put in writing textual content much more cleanly. Contemplate attempting to embed JavaScript or SQL code right into a Java string; you’ll want double quote marks incessantly, and in an everyday type of string these must be “escaped.” Nevertheless, with the textual content block, you possibly can keep away from escaping—except you want three double quote marks in a row; then you definately’ll want to flee not less than one among them.

Choice D reveals a single backslash adopted by an area (after which one other backslash adopted by s).

A backslash should introduce an accepted escape sequence, and you might be most likely acquainted with a few of the most typical ones: n (line feed), t (horizontal tab), and , which represents a single backslash within the ensuing string. This sequence could be any a type of specified within the Java Language Specification, part 3.10.7.

Nevertheless, this part expressly says the next:

It’s a compile-time error if the character following a backslash in an escape sequence isn’t a Line Terminator or an ASCII b, s, t, n, f, r, “, ‘, , 0, 1, 2, 3, 4, 5, 6, or 7.

In view of this, you possibly can see that the backslash in possibility D, adopted as it’s by the house character, is an invalid mixture; thus, possibility D is inaccurate.

Choice E is right and demonstrates the long-standing method for including a double quote mark right into a string. Whereas this was the one method to obtain this previous to the textual content block, it’s not obligatory to flee one (or two sequential) double quote marks in a textual content block. If you need three double quote marks in a row, you should escape not less than one among them, or they are going to be taken as the top of the block. Discover that the string that ends in possibility E will likely be an identical to that of possibility C.

As well as, Choice F is right and reveals a method so as to add three consecutive double quote marks inside a textual content block. As talked about within the dialogue about possibility E, you possibly can escape the primary, the second, or the third double quote mark—or, certainly, any mixture of them. All these approaches are legitimate and an identical, if there are usually not three unescaped double quote marks in sequence (which might terminate the block). Subsequently, the next variants are additionally legitimate:

// good too

var v6 = “””

         “””

         “””;

// the identical as earlier

var v6 = “””

         “””

         “””;

Conclusion. The proper solutions are choices C, E, and F.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments