Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Strings and StringBuilder
What will be the output of this code?
var sb = new System.Text.StringBuilder("abc");
sb.Append("def");
sb.Remove(2, 3);
Console.WriteLine(sb.ToString());
Aabf
Babdef
Cab
Dabcdef
Step-by-Step Solution
Solution:
  1. Step 1: Trace Append and Remove operations

    Initial string is "abc". Append adds "def" making "abcdef". Remove(2,3) deletes 3 chars starting at index 2: removes "cde".
  2. Step 2: Determine resulting string

    After removal, string is "ab" + "f" = "abf".
  3. Final Answer:

    abf -> Option A
  4. Quick Check:

    Remove deletes substring correctly = abf [OK]
Quick Trick: Remove(start, count) deletes substring from StringBuilder [OK]
Common Mistakes:
MISTAKES
  • Miscounting removed characters
  • Assuming Remove deletes only one char

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes