Friday, May 23, 2014

Strings of C#.Net Interview Questions(28)

Strings of C#.Net Interview Questions(28)





1.Define String?
A)String is a collection of characters. It is an combination of numericals, characters and special characters. In C#.Net string acts as a reference type. It can be conceived by “Using System. String”.

2. What is the difference between string and String?
A) No difference.

3. List the different types of operations of strings?
A)

4. What are the different forms of string?
A) Strings can be categorized into number of forms. They are:
                     Ø  Mutable
                     Ø  Immutable

5. What is Mutable?
A)Mutable means the objects can be changed.StringBuilder class belongs to mutable type.

6. What is Immutable?
A) Immutable means the objects can’t be changed.String class belongs to immutable type.

7. Is String belongs to mutable or immutable?
A)String belongs to immutable.

8.What is the purpose of StringBuilder?
A) StringBuilder is a class belongs to mutable type.This class can be used when we want to modify a string without creating a new object.Using StringBuilder class it improves the performance when concatenating many strings in an iterations or in a loop.

9.How can we Instantiate StringBuilder Class?
A) StringBuilder objectname=new StringBuilder(“”);
    E.G: StringBuilder ss=new StringBuilder(“Welcome to DotnetInterviewQuestion”);

10.How can we modify by using StringBuilder class?
A)By using the following methods we can modify the contents of a stringbuilder.
                    Ø  Append()
                    Ø  AppendFormat()
                    Ø  Insert()
                    Ø  Remove()
              Ø  Replace()
11. Can we convert StringBuilder object to a string?
A) Yes. We can convert StringBuilder object to string.By using ToString() method we can convert stringbuilder value to string.
E.G: StringBuilder sb=new StringBuilder(“I Love”);
       sb.Append(“My Country”);
       Console.WriteLine(sb.ToString());

12.What is the difference between Convert.toString and .ToString()?
A) Syntax:Convert.ToString(variablename);
                   Variablename.ToString();
                Primary difference between Convert.toString and .ToString() is Convert.ToString() method handles null values but .ToString() method  it won’t allows null values it throws an null reference exception error.Good programming practices suggests Convert.ToString() instead of .ToString().

13. What is the difference between System.String and System.StringBuilder classes?
A)System.String is immutable.It can’t manipulate the data. System.StringBuilder can have mutable string where a variety of operations can be performed.It can manipulate the data.

14. String objects are immutable?Infer the statement?
A)String objects are immutable means, they cannot be alter they have been declared. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of “a” and “b” are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable “a”, and the original object that was assigned to “a” is released for garbage collection because no other variable holds a reference to it.

string a = "First String ";
string b = "Second String";
// Concatenate “a” and “b”. This actually creates a new
// string object and stores it in a, releasing the
// reference to the original object.
a += b;
System.Console.WriteLine(a);
// Output: First String Second String

15. What's the advantage of using System.Text.StringBuilder over System. String?
A) String Builder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it's being operated on a new instance is created.

16.Is there any difference between string keyword and System.String class?
A)No there is no difference between string keyword and System.String class.string keyword is an alias for System.String class.we can use any naming convention according to our usability levels.The String class provides many methods for safely creating, manipulating, and comparing strings.

17. What is a verbatim string literal and why do we use it?
A)The "@" symbol is the verbatim string literal. Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string. The following example shows some common uses for verbatim strings:

string ImagePath = @"C:\Images\Buttons\SaveButton.jpg";
//Output: C:\Images\Buttons\SaveButton.jpg

string MultiLineText = @"This is multiline
Text written to be in
three lines.";
/* Output:
This is multiline
Text written to be in
three lines.
*/

string DoubleQuotesString = @"My Name is "Naga Veerendra Parvataneni";
//Output: My Name is "Naga Veerendra Parvataneni".

18.How can we initializes a string in different forms?
A)
Form1:string  stringname=””;
E.G:string  a=”Welcome”;

Form2:String[]  objectname=new String[]{“”,””,””};
E.G:String[]  a=new String[]{”C#.Net”,”ADO.Net”,”Asp.Net”};
Form3:String  objectname=new String{new[]{“”,””,””}};
E.G:String[]  a=new String(new []{”C#.Net”,”ADO.Net”,”Asp.Net”});
It is used for a character literals.

19.Is String belongs to a reference type or value type?
A)String is a reference type.It holds the address of each char in the string.

20.Is char belongs to a reference type or value type?
A)char belongs to a value types.It can holds a value only.
21. What does the “@” symbol mean when it appears before the opening double quote of a string?
A)”@” symbol is used to instruct the compiler to ignore the actual meaning of a symbol or a keyword.For example “\t” is a escape sequence it’s meaning is to provide a single tab space if we use “@” preceded to “\t” escape sequence it won’t provides a single tab space.So that means that if you want to display an escape sequence as a text and not to work as an escape sequence, then the @ symbol can do the work for you.Generally this type of declaration is used in Files and Streams.To display the file paths.
Example:
”C:\\Folder\\veerendra.txt” as
 
22.Define Carriage Return?
A)Carriage Return is one of the escape sequence in C#.Net.It is often called as CR represented with “\r” it is used to refers to a control character or mechanism used to reset a device ‘s position to the beginning of a line of text.

23.What is an escape sequence?Why do we use backslash characters with strings?
A)An escape sequence has an essential mechanism to escape from the rules of a character’s  regular role and behave differently to achieve a different purpose.It is achieved by”\”(slash) character.Some escape sequences are listed below.

24. What does F2 in ToString(“F2”) mean?
A) The letter F instructs the compiler that the output number string would be displayed up to some fixed point places after the decimal dot. The 2 after the F signifies that the output has to be up to 2 places after the decimal dot, and the number has to be rounded.
For example:
double d = 45.3920002;
string s = d.ToString(“F2”);
The output of this would be
45.39

25. How can you display the number 5555 of type int, as 0005555 in the output?
A)This may be achieved by formatting the int type. The int firstly needs to be converted to string, and then formatted for the output. See the code example below:
Int I = 5555;
string s = i.ToString(“D7”);
Note that in the value D7, D means that the output should be displayed as a decimal, and 7 means the output should have at least 7 numerals. Now here, as the variable ‘I’ had only 4 digits, so 3 more 0’s are padded towards the left of the output.

26 .How can we display a decimal value as a currency string?
A)In C#.Net many approaches are there.Here we will use “C” is used to convert any value into currency type.Following example explores the concept:
decimal d = 121252525.858285m;
          string ss = d.ToString("C");
          Console.WriteLine(ss);
O/P:$121,252,525.86

27.How to display a number as a percent?
A)A number with any of data type  like int or float or double or decimal we can display % during the output.By using “P” we can display the number successedded with % symbol.
E.G:                   decimal d = 12m;
            string ss = d.ToString("P");
            Console.WriteLine(ss);
            O/P:1,200.00%

28.How to represent a number in Exponential format?
A)By Using “E”.
            decimal d = 12m;
            string ss = d.ToString("E");
            Console.WriteLine(ss);
            O/P:1.200000E+001

29.How to represent a number in hexadecimal string?
A)By Using “X”.
In hexadecimal conversion consider only an integer types others won’t be allowed like double,decimal etc it throws an exception invalid format.
E.G:
                       int i = 123;
            string ss = i.ToString("X");
            Console.WriteLine(ss);
                       O/P:7B

0 comments:

Post a Comment