C# Multiple Choice Questions & Answers on Char Types and String Literals for Freshers
https://www.computersprofessor.com/2018/01/c-multiple-choice-questions-answers-on_3.html
1. What is the Size of ‘Char’ datatype?
a) 8 bit
b) 12 bit
c) 16 bit
d) 20 bit
Answer: c
2. Choose the output for the following set of code.
static void Main(string[] args)
{char c = 'g';
string s = c.ToString();
string s1 = "I am a human bein" + c;
Console.WriteLine(s1);
Console.ReadLine();
}
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
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.
Output: I am a human being.
3. Given is the code of days(example:”MTWTFSS”) which i need to split and hence create a list of days of week in strings(example:”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”,”Sunday”). A set of code is given for this purpose but there is error ocuring in that set of code related to conversion of char to strings.Hence,Select a code to solve the given error.
static void Main(string[] args)
{var days = "MTWTFSS";
var daysArray = days.ToCharArray().Cast<string>().ToArray();
for (var i = 0; i < daysArray.Length; i++)
{switch (daysArray[i])
{case "M":
daysArray[i] = "Monday";
break;
case "T":
daysArray[i] = "Tuesday";
break;
case "W":
daysArray[i] = "Wednesday";
break;
case "R":
daysArray[i] = "Thursday";
break;
case "F":
daysArray[i] = "Friday";
break;
case "S":
daysArray[i] = "Saturday";
break;
case "U":
daysArray[i] = "Sunday";
break;
}}daysArray[daysArray.Length - 1] = "and " + daysArray[daysArray.Length - 1];
Console.WriteLine(string.Join(", ", daysArray));
}
a) var daysArray = new List();
b) var daysArray = days.Select(c =>dayMapping[c]).ToArray();
c) var daysArray = days.ToCharArray().Select(c =>c.Tostring()).ToArray();
d) None of above mentioned.
b) var daysArray = days.Select(c =>dayMapping[c]).ToArray();
c) var daysArray = days.ToCharArray().Select(c =>c.Tostring()).ToArray();
d) None of above mentioned.
Answer: c.
Explanation : The problem arises due to cast conversion from “char” to “string” as one is not inherited from other. So, quick way of conversion is just using Char.ToString().
4. Output for the following set of code is ?
static void Main(string[] args)
{{var dayCode = "MTWFS";
var daysArray = new List<string>();
var list = new Dictionary<string, string>
{ {"M", "Monday"}, {"T", "Tuesday"}, {"W", "Wednesday"},
{"R", "Thursday"}, {"F", "Friday"}, {"S", "Saturday"},
{"U", "Sunday"}
};
for (int i = 0,max = dayCode.Length; i < max; i++)
{var tmp = dayCode[i].ToString();
if (list.ContainsKey(tmp))
{daysArray.Add(list[tmp]);
}}Console.WriteLine(string.Join("\n ", daysArray));
}
a) Monday ,Tuesday ,Wednesday ,Friday ,Saturday ,Sunday
b) Monday
Tuesday
Wednesday
Friday
Sunday
c) Monday
Tuesday
Wednesday
Friday
Saturday
d) Monday ,Tuesday ,Wednesday ,Friday ,Saturday
b) Monday
Tuesday
Wednesday
Friday
Sunday
c) Monday
Tuesday
Wednesday
Friday
Saturday
d) Monday ,Tuesday ,Wednesday ,Friday ,Saturday
Answer: c.
5. Select the correct differences between char and varchar datatypes?
1. varchar is non unicode and char is unicode character datatype
2. char is ‘n’ bytes whereas varchar is actual length in bytes of data entered in terms of storage size
3. varchar is variable in length and char is the fixed length string
4. 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) 1, 3, 4
b) 2, 3, 4
c) 1, 2, 4
d) 3, 4
Answer: d.
6. 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 length of strings in considerations.
7. Correct output for the given C#.NET code is ?
static void Main(string[] args)
{string s1 = "Delhi";
string s2;
s2 = s1.Insert (6, "Jaipur");
Console.WriteLine(s2);
}
a) DelhJaipuri
b) Delhi Jaipur
c) Delhi
d) DelhiJaipur
b) Delhi Jaipur
c) Delhi
d) DelhiJaipur
Answer: d.
Explanation:Insert method() of string class used to join two strings s1 and s2.
8. For two strings s1 and s2 to be equal, which is the correct way to find if the contents of two strings are equal ?
a) if(s1 = s2)
b) int c;
c = s1.CompareTo(s2);
c) if (s1 is s2).
d) if(strcmp(s1, s2))
Answer : b
9. Select the output for given string ?
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);
Console.ReadLine();
}
Enter a String:BOMBAY.
a) BOMBA
b) YABMOB
c) BOMAYB
d) YABMO
Answer : b.
Explanation : Explain the concept of reversal of string without using any string inbuilt method but using while loop conditions.
Output:YABMOB
10. Select the appropriate set of code for conversion of string to hexa form.
static void Main(string[] args)
{string teststring = "MIKA@?&";
string hex = ConvertstringToHex(teststring, system.Text.Encoding.Unicode);
Console.WriteLine(hex);
}
a)
static string ConvertstringToHex(string input, system.Text.Encoding encoding)
{string Bytes = encoding.GetBytes(input);
string Builder sbBytes = new Builder sbBytes();
for each (byte 'b' in StringBytes)
{sBytes.AppendFormat("{0:x2}", b);
}return sbBytes.Tostring();
}
b)
static string ConvertstringToHex(string input, system.Text.Encoding encoding)
{char[]string Bytes = encoding.GetBytes(input);
string Builder sbBytes = new Builder sbBytes(StringBytes.Length*2);
for each (byte 'b' in StringBytes)
{sBytes.AppendFormat("{0:X2}", b);
}return sbBytes.Tostring();
}
c)
public static string ConvertStringToHex(String input, System.Text.Encoding encoding)
{{Byte[] stringBytes = encoding.GetBytes(input);
StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
foreach (byte b in stringBytes)
{sbBytes.AppendFormat("{0:X2}", b);
}Console.WriteLine(sbBytes.ToString());//sbBytes.ToString());
return sbBytes.ToString();
}}
d) None of the mentioned.
Answer : c.
Output:public static string ConvertStringToHex (String input, System.Text.Encoding encoding)
{
{
Byte[] stringBytes = encoding.GetBytes(input);
StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
foreach (byte b in stringBytes)
{
sbBytes.AppendFormat(“{0:X2}”, b);
}
Console.WriteLine(sbBytes.ToString());//sbBytes.ToString());
return sbBytes.ToString();
}
11. Which of the following code is used for conversion of hex to string form ?
static void Main(string[] args)
{string testString = "MIKA@?&^";
string normal = ConvertHexToString (hex, System.Text.Encoding.Unicode);
Console.WriteLine(normal);
Console.ReadLine();
}
a)
public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
{char[] numberChars = hexInput.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2)
{bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 0), 16);
}return encoding.GetString(bytes);
}
b)
public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
{int numberChars = hexInput.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2)
{bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
}return encoding.GetString(bytes);
}
c)
public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
{string numberChars = hexInput.Length;
byte[] bytes = new byte[numberChars];
for (int i = 0; i < numberChars; i += 2)
{bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
}return encoding.GetString(bytes);
}
d) None of the mentioned.
Answer : b
Output:
public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
{
int numberChars = hexInput.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2) { bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16); } return encoding.GetString(bytes); } [/expand] 12. Correct output for the C#.NET code given below is :
string s1 = " I AM BEST ";
string s2;
s2 = s1.substring (5, 4);
Console.WriteLine (s2);
a) AM BEST
b) I AM BES
c) BEST
d) I AM
b) I AM BES
c) BEST
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.
13. 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
14. 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
15. Why strings are of reference type in C#.NET ?
a) To create string on stack
b) To reduce 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.
