Posts Tagged ‘C Sharp Question 5’

C Sharp Question 5

You are developing a console application that uses .NET Framework text processing libraries. You write the following code to process text:

StringBuilder sb = new StringBuilder(5);
sb.Append(“01234567890123456789″);
sb.Length = 10;
sb.Append(“AB”);
Console.WriteLine(“Length = {0}”, sb.Length);
Console.WriteLine(“Capacity = {0}”, sb.Capacity);

You must determine the value of sb.Length and sb.Capacity printed by this code segment.

Which values should you select?

C Sharp Question 5

C Sharp Question 5

The correct answer is:

Length = 12
Capacity = 20

The StringBuilder object is first created with a capacity of 5. If needed, the Append method increases the length and capacity of the StringBuilder object. Therefore, the following statement increases the capacity and length of the StringBuilder object to 20:

sb.Append(“01234567890123456789″);

When the following statement is executed, the length of the StringBuilder object is truncated to 10 characters, but the capacity stays at 20:

sb.Length = 10;

Finally, the following statement increases length of the StringBuilder object by 2 characters to 12:

sb.Append(“AB”);

Capacity stays at 20 because there is still room to grow.

Objective:
 Implementing globalization, drawing, and text manipulation functionality in a .NET Framework application

Sub-Objective:
7.3 Enhance the text handling capabilities of a .NET Framework application (refer System.Text namespace), and search, modify, and control text within a .NET Framework application by using regular expressions. (Refer System.RegularExpressions namespace)

Be the first to comment - What do you think?  Posted by admin - March 9, 2010 at 5:20 pm

Categories: Certification   Tags: ,