Courseiva
Question 298 of 481
Primitives, Strings and OperatorshardMultiple ChoiceObjective-mapped

String Immutability: Why concat() and replace() Don't Modify Original

Given: String str = "Java"; str = str.concat(" SE"); str.replace('a', 'A'); System.out.println(str); What is the output?

Quick Answer

The answer is Java SE. This output occurs because String immutability in Java means that methods like concat() and replace() never modify the original String object; instead, they return a brand new String. When str.concat(" SE") is called, it creates the new String "Java SE", and because that result is explicitly assigned back to str, the variable now points to the new object. However, the subsequent replace('a', 'A') also returns a new String, but since its return value is not captured or assigned, it is discarded, leaving str unchanged as "Java SE". On the Oracle Java Foundations 1Z0-811 exam, this question tests your understanding that all String methods produce new objects without altering the original, a common trap where students assume replace() modifies the string in place. Remember the key tip: if you don’t assign the result of a String method, the change is lost—think of it as “no assignment, no change.”

⚠ Common exam trap

A common mix-up: candidates assume `replace` modifies the original string in place, forgetting that String methods return a new object and the result must be assigned to affect the variable.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Java SE

The `concat` method returns a new String "Java SE" which is assigned back to `str`. The `replace` method also returns a new String but its result is not assigned to any variable, so the original `str` remains unchanged. The final `println` outputs the current value of `str`, which is "Java SE".

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Java

    Why it's wrong here

    Incorrect because concat result is assigned.

  • JAVA SE

    Why it's wrong here

    Incorrect.

  • Java SE

    Why this is correct

    The output is "Java SE" because Java String objects are immutable. The `str.concat(" SE")` operation creates a new String "Java SE" and successfully reassigns `str` to refer to this new object. However, `str.replace('a', 'A')` also creates a new String ("JAvA SE"), but its return value is not assigned back to `str`. Consequently, `str` continues to refer to the "Java SE" object established by the `concat` method, which is then printed to the console.

  • JAvA SE

    Why it's wrong here

    Incorrect because replace not assigned.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on 1Z0-811

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer writes: String s = "Hello"; s.concat(" World"); System.out.println(s); What is the output?

medium
  • A.Compilation fails
  • B.Hello World
  • C.Hello
  • D.Hello World

Why C: Strings in Java are immutable. The `concat()` method returns a new string but does not modify the original string `s`. Since the return value is not assigned to any variable, the original string `s` remains unchanged, so `System.out.println(s)` prints "Hello".

Last reviewed: Jun 11, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.