Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy

Question 24

Question ID: UKOCP16290


Consider below code of Test.java file:


What will be the result of compiling and executing Test class?

  • A THERMAL:ELECTROTHERMAL
  • B ELECTRO:ELECTROTHERMAL
  • C ELECTROTHERMAL:THERMAL
  • D ELECTROTHERMAL:ELECTRO
  • E ELECTRO:ELECTRO
  • F ELECTRO:THERMAL
  • G

    THERMAL:ELECTRO

  • H

    THERMAL:THERMAL

Reveal correct answer

Correct answer: E

Explanation

UKOCP16290:

strip() method of String class (available since Java 11) returns a string whose value is this string, with all leading and trailing white space removed.

To find out what is white space character in Java, check Character.isWhitespace​(int) method, you will find below:

A character is a Java whitespace character if and only if it satisfies one of the following criteria:

It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').

It is '\t', U+0009 HORIZONTAL TABULATION.

It is '\n', U+000A LINE FEED.

It is '\u000B', U+000B VERTICAL TABULATION.

It is '\f', U+000C FORM FEED.

It is '\r', U+000D CARRIAGE RETURN.

It is '\u001C', U+001C FILE SEPARATOR.

It is '\u001D', U+001D GROUP SEPARATOR.

It is '\u001E', U+001E RECORD SEPARATOR.

It is '\u001F', U+001F UNIT SEPARATOR.


White space character in java includes space character along with above characters.


At Line n1, 'sb' refers to StringBuilder object {'E','L','E','C','T','R','O','T','H','E','R','M','A,'L'}. So length of this StringBuilder object is 14.

At Line n2, sb.setLength(7); sets the length of StringBuilder object referred by 'sb' to 7 (it is reducing the length of StringBuilder object from 14 to 7), hence 'sb' refers to {'E','L','E','C','T','R','O'}. Last 7 characters are gone.

There is no white space character in above StringBuilder object, hence at Line n3 sb.toString() returns "ELECTRO" and strip() method at Line n3 returns "ELECTRO".

"ELECTRO" is printed on to the console.

When code reaches Line n4, console shows "ELECTRO:"


At Line n5, sb.setLength(14); sets the length of StringBuilder object referred by 'sb' to 7 (it is increasing the length of StringBuilder object by 7 by filling available space with null characters '\u0000'). Now 'sb' refers to StringBuilder object {'E','L','E','C','T','R','O','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000,'\u0000'}.

At Line n6, sb.toString() returns corresponding String object "ELECTRO       ". As '\u0000' is white space character, hence "ELECTRO       ".strip() returns "ELECTRO". Line n6 prints "ELECTRO" on to the console.


Hence, the output is ELECTRO:ELECTRO.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need