Question:

What will be the output of the following Java code?

public class Hello {
 public static void main(String[] args) {
    String str = "Hello";
    str.concat(" World");
    System.out.println(str);
  }
}

Show Hint

Strings are immutable. concat returns a new string; without assigning it back, str is unchanged.
Updated On: Jul 2, 2026
  • Hello
  • Hello World
  • Compilation Error
  • Hello World Hello
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Step 1: Strings in Java are immutable. A method like concat cannot change the original string.

Step 2: str.concat(" World") builds a brand new string "Hello World" and returns it.

Step 3: That returned value is not stored anywhere. The variable str still points to "Hello".

Step 4: So println prints Hello. Answer is (A).
Was this answer helpful?
0
0