Challenge - 5 Problems
StringBuilder Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this StringBuilder code?
Consider the following C# code using StringBuilder. What will be printed to the console?
C Sharp (C#)
using System; using System.Text; class Program { static void Main() { StringBuilder sb = new StringBuilder("Hello"); sb.Append(", World!"); sb.Replace('o', '0'); Console.WriteLine(sb.ToString()); } }
Attempts:
2 left
💡 Hint
Remember that Replace changes all occurrences of the character.
✗ Incorrect
The Replace method changes all 'o' characters to '0'. Both 'o's in "Hello" and "World" are replaced, so the output is "Hell0, W0rld!".
🧠 Conceptual
intermediate1:30remaining
Why use StringBuilder instead of string concatenation?
Which of the following best explains why StringBuilder is preferred over using string concatenation in a loop?
Attempts:
2 left
💡 Hint
Think about how strings behave when changed in C#.
✗ Incorrect
Strings in C# are immutable, so concatenation creates new strings each time, which is slow and uses more memory. StringBuilder changes the content without creating new strings repeatedly.
🔧 Debug
advanced1:30remaining
What error occurs in this StringBuilder usage?
What error will this code produce when run?
C Sharp (C#)
using System; using System.Text; class Program { static void Main() { StringBuilder sb = null; sb.Append("Test"); Console.WriteLine(sb.ToString()); } }
Attempts:
2 left
💡 Hint
What happens if you call a method on a null object?
✗ Incorrect
The StringBuilder variable sb is null, so calling Append on it causes a NullReferenceException at runtime.
❓ Predict Output
advanced2:00remaining
What is the final string after these StringBuilder operations?
What will be the output of this program?
C Sharp (C#)
using System; using System.Text; class Program { static void Main() { StringBuilder sb = new StringBuilder("abc"); sb.Insert(1, "123"); sb.Remove(4, 2); sb.Append("xyz"); Console.WriteLine(sb.ToString()); } }
Attempts:
2 left
💡 Hint
Track each operation step by step on the string.
✗ Incorrect
Start: "abc"
Insert at index 1: "a123bc"
Remove 2 chars at index 4: removes 'b' and 'c' → "a123"
Append "xyz": "a123xyz"
🧠 Conceptual
expert2:30remaining
Why does StringBuilder improve performance in repeated string modifications?
Choose the best explanation for why StringBuilder improves performance when modifying strings repeatedly in C#.
Attempts:
2 left
💡 Hint
Think about how strings and StringBuilder store data internally.
✗ Incorrect
StringBuilder maintains a mutable character array internally, so it can change content without creating new strings. This reduces memory use and speeds up repeated modifications.