You are developing an application that will convert data into multiple output formats.The application includes the following code. (Line numbers are included for reference only.)
You are developing a code segment that will produce tab-delimited output. All output routines implement the
following interface:
You need to minimize the completion time of the GetOutput() method.
Which code segment should you insert at line 06?
A.
Option A
B.
Option B
C.
Option C
D.
Option D
Explanation:
A String object concatenation operation always creates a new object from the existing string and the new data.
A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is
appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original
buffer is copied to the new buffer, and the new data is then appended to the new buffer.
The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of
memory allocations. A String concatenation operation always allocates memory, whereas a StringBuilder
concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodatethe new data. Use the String class if you are concatenating a fixed number of String objects. In that case, the
compiler may even combine individual concatenation operations into a single operation. Use a StringBuilder
object if you are concatenating an arbitrary number of strings; for example, if you’re using a loop to concatenate
a random number of strings of user input.
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx
B
Indeed B!
StringBuilder minimizes memory usage not completion time. D is the right answer I believe
The Explanation indicates that StringBuilder is the correct answer:
Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you’re using a loop to concatenate a random number of strings of user input.
StringBuilder takes the lead in performance
Depends on how many strings must be added.
answer D is faster for 4 srings. Otherwise it is answer B.
https://www.dotnetperls.com/stringbuilder-performance
Here guys, made a some test code for you. I SAY B.
class FormatterTest
{
public static void Main()
{
IOutputFormatter tbf = new TabDelimitedFormatter();
List GG = new List();
for(int i = 0; i < 1000; i++)
{
GG.Add("Whad do hek");
}
tbf.GetOutput(GG.GetEnumerator(), 1000);
tbf.GetOutput2(GG.GetEnumerator(), 1000);
Console.ReadLine();
}
}
public class TabDelimitedFormatter : IOutputFormatter
{
private readonly Func suffix = col => col % 2 == 0 ? ‘\n’ : ‘\t’;
public string GetOutput(IEnumerator iterator, int recordSize)
{
Stopwatch stopwatch = new Stopwatch();
// Begin timing.
stopwatch.Start();
string output = null;
for (int i = 1; iterator.MoveNext(); i++)
{
output += iterator.Current + suffix(i);
}
stopwatch.Stop();
Console.WriteLine(“Time elapsed for +=: {0}”, stopwatch.Elapsed);
return output;
}
public string GetOutput2(IEnumerator iterator, int recordSize)
{
Stopwatch stopwatch = new Stopwatch();
// Begin timing.
stopwatch.Start();
var output = new StringBuilder();
for (int i = 1; iterator.MoveNext(); i++)
{
output.Append(iterator.Current);
output.Append(suffix(i));
}
stopwatch.Stop();
Console.WriteLine(“Time elapsed: {0}”, stopwatch.Elapsed);
return output.ToString();
}
}
public interface IOutputFormatter
{
string GetOutput(IEnumerator iterator, int recordSize);
string GetOutput2(IEnumerator iterator, int recordSize);
}
Answer D. In my experience of MCP exams, if 3 answers are really similar, you can bet the correct answer is one of them.