Bird
0
0

What will be printed by this code?

medium📝 Predict Output Q5 of 15
Java - Strings and String Handling
What will be printed by this code?
StringBuffer sb = new StringBuffer("Hello");
sb.insert(5, " World");
System.out.println(sb.toString());
AHelloWorld
B WorldHello
CHello World
DHello WorldHello
Step-by-Step Solution
Solution:
  1. Step 1: Understand insert method

    Inserts " World" at index 5, which is after "Hello".
  2. Step 2: Resulting string

    String becomes "Hello World".
  3. Final Answer:

    Hello World -> Option C
  4. Quick Check:

    Insert at index 5 = Hello World [OK]
Quick Trick: insert(index, text) adds text at given position [OK]
Common Mistakes:
  • Assuming insert adds at start by default
  • Ignoring space in inserted string
  • Confusing insert with append

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes