0
0
C Sharp (C#)programming~20 mins

StringBuilder methods and performance in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
StringBuilder Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of StringBuilder Append and Insert
What is the output of this C# code snippet?
C Sharp (C#)
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("Hello");
        sb.Append(" World");
        sb.Insert(5, ",");
        Console.WriteLine(sb.ToString());
    }
}
AHello, World
BHello World,
CHello World
DHello,World
Attempts:
2 left
💡 Hint
Remember that Insert adds characters at the specified index without removing existing ones.
Predict Output
intermediate
2:00remaining
StringBuilder Replace method effect
What will be printed by this code?
C Sharp (C#)
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("abracadabra");
        sb.Replace("a", "o", 3, 5);
        Console.WriteLine(sb.ToString());
    }
}
Aabrocodobro
Babrocodobra
Cobrocodobro
Dobrocadobra
Attempts:
2 left
💡 Hint
Replace only affects the substring starting at index 3 for length 5.
Predict Output
advanced
2:00remaining
Performance difference between string concatenation and StringBuilder
What is the expected output of this code snippet?
C Sharp (C#)
using System;
using System.Text;
using System.Diagnostics;

class Program {
    static void Main() {
        int n = 10000;
        Stopwatch sw = new Stopwatch();

        sw.Start();
        string s = "";
        for (int i = 0; i < n; i++) {
            s += i.ToString();
        }
        sw.Stop();
        Console.WriteLine("String concat time: " + sw.ElapsedMilliseconds + "ms");

        sw.Reset();
        sw.Start();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.Append(i.ToString());
        }
        string result = sb.ToString();
        sw.Stop();
        Console.WriteLine("StringBuilder time: " + sw.ElapsedMilliseconds + "ms");
    }
}
AString concat time: much greater than StringBuilder time
BString concat time: much less than StringBuilder time
CBoth times are about the same
DCode throws an exception due to memory overflow
Attempts:
2 left
💡 Hint
String concatenation creates many intermediate strings, while StringBuilder modifies in place.
Predict Output
advanced
2:00remaining
Effect of Capacity and Length on StringBuilder
What will be the output of this code?
C Sharp (C#)
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder(5);
        Console.WriteLine(sb.Capacity);
        sb.Append("HelloWorld");
        Console.WriteLine(sb.Capacity);
        sb.Length = 5;
        Console.WriteLine(sb.ToString());
    }
}
A
5
20
HelloWorld
B
5
10
Hello
C
5
16
Hello
D
5
16
HelloWorld
Attempts:
2 left
💡 Hint
Capacity grows automatically when exceeded. Length truncates the string.
Predict Output
expert
2:00remaining
StringBuilder Thread Safety and Exception
What happens when this code runs?
C Sharp (C#)
using System;
using System.Text;
using System.Threading.Tasks;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder();
        Parallel.For(0, 1000, i => {
            sb.Append(i);
        });
        Console.WriteLine(sb.Length);
    }
}
APrints a number close to 2893 without error
BThrows a System.ArgumentOutOfRangeException
CThrows a System.IndexOutOfRangeException
DThrows a System.InvalidOperationException
Attempts:
2 left
💡 Hint
StringBuilder is not thread-safe for concurrent writes.