Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Java - Strings and String Handling
What will this code print?
String s = "Hello";
s = s + " World";
System.out.println(s);
AHello
BHello World
CHelloWorld
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand string concatenation and reassignment

    The + operator joins strings, and s is reassigned to the new combined string.
  2. Step 2: Evaluate the output

    "Hello" + " World" results in "Hello World" which is printed.
  3. Final Answer:

    Hello World -> Option B
  4. Quick Check:

    Concatenation with + adds strings [OK]
Quick Trick: Strings are immutable; + creates new string [OK]
Common Mistakes:
  • Expecting original string unchanged
  • Missing space in concatenation
  • Confusing + with other operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes