nerdexam
Oracle

1Z0-803 · Question #95

What is the result? public class StringReplace { public static void main(String[] args) { String message = "Hi everyone!"; System.out.println("message = " + message.replace("e", "X")); } }

The correct answer is B. message = Hi XvXryonX!. The String.replace() method replaces all occurrences of the specified character sequence 'e' with 'X' in the original string "Hi everyone!".

Working with Selected Classes from the Java API

Question

What is the result? public class StringReplace { public static void main(String[] args) { String message = "Hi everyone!"; System.out.println("message = " + message.replace("e", "X")); } }

Options

  • Amessage = Hi everyone!
  • Bmessage = Hi XvXryonX!
  • CA compile time error is produced.
  • DA runtime error is produced.
  • Emessage =
  • Fmessage = Hi Xveryone!

How the community answered

(38 responses)
  • A
    3% (1)
  • B
    89% (34)
  • D
    3% (1)
  • E
    5% (2)

Why each option

The `String.replace()` method replaces all occurrences of the specified character sequence 'e' with 'X' in the original string "Hi everyone!".

Amessage = Hi everyone!

This choice incorrectly suggests that no replacement occurs, failing to apply the `replace()` method's functionality.

Bmessage = Hi XvXryonX!Correct

The `replace("e", "X")` method on the string "Hi everyone!" replaces all three occurrences of the lowercase character 'e' with 'X', yielding "Hi XvXryonX!".

CA compile time error is produced.

The code is syntactically correct and will compile successfully without any compilation errors.

DA runtime error is produced.

The code does not contain any operations that would typically lead to a runtime error in this simple string manipulation.

Emessage =

This choice incorrectly implies that the string becomes empty or significantly altered beyond character replacement.

Fmessage = Hi Xveryone!

This choice incorrectly suggests that only a single or partial replacement of 'e' occurs; `replace()` replaces all occurrences.

Concept tested: Java String.replace() method

Source: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#replace(java.lang.CharSequence,java.lang.CharSequence)

Topics

#String class#string manipulation#replace method

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice