C# Programming Questions and Answers Part-3

1. What is the Size of ‘Char’ datatype?
a) 8 bit
b) 12 bit
c) 16 bit
d) 20 bit

Answer: c
Explanation: Size of ‘Char’ datatype is 16 bit.

2. What will be the output of the following C# code?
static void Main(string[] args)
char c = 'g';
string s = c.ToString();
string s1 = "I am a human being" + c;
a) I am a human bein c
b) I am a human being
c) I am a human being c
d) I am a human bein

Answer: b
Explanation: ‘g’is stored in character variable ‘c’ which later on is converted to string using method Convert.Tostring() and hence appended at last of the string in s1.

3. Select the correct differences between char and varchar data types?
i. varchar is non unicode and char is unicode character data type
ii. char is ‘n’ bytes whereas varchar is actual length in bytes of data entered in terms of storage size
iii. varchar is variable in length and char is the fixed length string
iv. For varchar, if a string is less than the maximum length then it is stored in verbatim without any extra characters while for char if a string is less than the set length it is padded with extra characters to equalize its length to given length
a) i, iii, iv
b) ii, iii, iv
c) i, ii, iv
d) iii, iv

Answer: d
Explanation: By definition.

4. Which is the String method used to compare two strings with each other?
a) Compare To()
b) Compare()
c) Copy()
d) ConCat()

Answer: b
Explanation: Compare() used to compare two strings by taking the length of strings in considerations.

5. What will be the output of the following C# code?
static void Main(string[] args)
string s1 = "Delhi";
string s2;
s2 = s1.Insert (6, "Jaipur");
a) DelhJaipuri
b) Delhi Jaipur
c) Delhi
d) DelhiJaipur

Answer: d
Explanation: Insert method() of string class used to join two strings s1 and s2.

6. What will be the output of the following C# string? (Enter a String : BOMBAY).
static void Main(string[] args)
string Str, Revstr = " ";
int Length;
Console.Write("Enter A String : ");
Str = Console.ReadLine();
Length = Str.Length - 1;
while (Length >= 0)
Revstr = Revstr + Str[Length];
Length --;
Console.WriteLine("Reverse String Is {0}", Revstr);

Answer: b
Explanation: Explain the concept of reversal of string without using any string inbuilt method but using while loop conditions.

7. What will be the output of the following C# code?
string s1 = " I AM BEST ";
string s2;
s2 = s1.substring (5, 4);
Console.WriteLine (s2);
d) I AM

Answer: c
Explanation: Substring() of string class used to extract substrings from given string. In the given substring condition, it extracts a substring beginning at 5th position and ending at 4th position.

8. Correct statement about strings are?
a) a string is created on the stack
b) a string is primitive in nature
c) a string created on heap
d) created of string on a stack or on a heap depends on the length of the string

Answer: c
Explanation: A string created on heap

9. Verbatim string literal is better used for?
a) Convenience and better readability of strings when string text consist of backlash characters
b) Used to initialize multi-line strings
c) To embed a quotation mark by using double quotation marks inside a verbatim string
d) All of the mentioned

Answer: d
Explanation: All of the mentioned

10. Why strings are of reference type in C#.NET?
a) To create string on stack
b) To reduce the size of string
c) To overcome problem of stackoverflow
d) None of the mentioned

Answer: b
Explanation: The problem of stack overflow very likely to occur since transport protocol used on web these days are ‘HTTP’ and data standard as ‘XML’. Hence, both make use of strings extensively which will result in stack overflow problem. So, to avoid this situation it is good idea to make strings a reference type and hence create it on heap.