Users Online

· Guests Online: 44

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Questions & Answers ? Char Types and String Literals

C# Questions & Answers – Char Types and String Literals

 

 

 

This section of our 1000+ C# multiple choice questions focuses on datatypes in character and strings in C# Programming Language.

 

 

 

 

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?

  1.  static void Main(string[] args)
  2.  {
  3.      char c = 'g';
  4.      string s = c.ToString();
  5.      string s1 = "I am a human being" + c;
  6.      Console.WriteLine(s1);
  7.      Console.ReadLine();
  8.  }

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.
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 the error occurring in that set of code related to the conversion of char to strings. Hence, Select a C# code to solve the given error.

  1. static void Main(string[] args)
  2. {
  3.     var days = "MTWTFSS";
  4.     var daysArray = days.ToCharArray().Cast<string>().ToArray();
  5.     for (var i = 0; i < daysArray.Length; i++)
  6.     {
  7.         switch (daysArray[i])
  8.         {
  9.         case "M":
  10.             daysArray[i] = "Monday";
  11.             break;
  12.         case "T":
  13.             daysArray[i] = "Tuesday";
  14.             break;
  15.         case "W":
  16.             daysArray[i] = "Wednesday";
  17.             break;
  18.         case "R":
  19.             daysArray[i] = "Thursday";
  20.             break;
  21.         case "F":
  22.             daysArray[i] = "Friday";
  23.             break;
  24.         case "S":
  25.             daysArray[i] = "Saturday";
  26.             break;
  27.         case "U":
  28.             daysArray[i] = "Sunday";
  29.             break;
  30.         }
  31.     }
  32.     daysArray[daysArray.Length - 1] = "and " + daysArray[daysArray.Length - 1];
  33.     Console.WriteLine(string.Join(", ", daysArray));
  34. }

a) var daysArray = new List<String>();
b) var daysArray = days.Select(c =>dayMapping[c]).ToArray();
c) var daysArray = days.ToCharArray().Select(c =>c.Tostring()).ToArray();
d) var daysArray = days.Select<String>();

Answer: c
Explanation: The problem arises due to cast conversion from “char” to “string” as one is not inherited from others. So, quick way of conversion is just using Char.ToString().

 

 

 

 

 

4. What will be the output of the following C# code?

  1. static void Main(string[] args)
  2. {
  3.     { 
  4.         var dayCode = "MTWFS";
  5.         var daysArray = new List<string>();
  6.         var list = new Dictionary<string, string>
  7.         { {"M", "Monday"}, {"T", "Tuesday"}, {"W", "Wednesday"},
  8.           {"R", "Thursday"}, {"F", "Friday"}, {"S", "Saturday"},
  9.           {"U", "Sunday"}
  10.         };
  11.        for (int i = 0,max = dayCode.Length; i < max; i++)
  12.        {
  13.            var tmp = dayCode[i].ToString();
  14.            if (list.ContainsKey(tmp))
  15.            {
  16.                daysArray.Add(list[tmp]);
  17.             }
  18.        }
  19.        Console.WriteLine(string.Join("\n ", daysArray)); 
  20.  }

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

Answer: c
Explanation: None.
Output:

 

        Monday
        Tuesday
        Wednesday
        Friday
        Saturday

 

 

 

 

 

5. 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.

 

 

 

 

 

 

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 the length of strings in considerations.

 

 

 

 

 

 

7. What will be the output of the following C# code?

  1. static void Main(string[] args)
  2. {
  3.     string s1 = "Delhi";
  4.     string s2;
  5.     s2 = s1.Insert (6, "Jaipur");
  6.     Console.WriteLine(s2);
  7. }

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.
Output :

 

DelhiJaipur

 

 

 

 

 

 

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
Explanation: None.

 

 

 

 

 

 

 

9. What will be the output of the following C# string? (Enter a String : BOMBAY).

  1. static void Main(string[] args)
  2. {
  3.     string Str,  Revstr = " "; 
  4.     int Length;
  5.     Console.Write("Enter A String : ");
  6.     Str = Console.ReadLine();
  7.     Length = Str.Length - 1;
  8.     while (Length >= 0)
  9.     {
  10.         Revstr = Revstr + Str[Length];
  11.         Length --;
  12.     }
  13.     Console.WriteLine("Reverse  String  Is  {0}",  Revstr);
  14.     Console.ReadLine();
  15. }

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 C# code for conversion of string to hexa form.

  1.  static void Main(string[] args)
  2.  {
  3.      string teststring = "MIKA@?&";
  4.      string hex = ConvertstringToHex(teststring, system.Text.Encoding.Unicode);
  5.      Console.WriteLine(hex);
  6.  }

a)

  1. static string ConvertstringToHex(string input, system.Text.Encoding encoding)
  2. {
  3.     string Bytes = encoding.GetBytes(input);
  4.     string Builder sbBytes = new Builder sbBytes();
  5.     for each (byte 'b' in StringBytes)
  6.     {
  7.         sBytes.AppendFormat("{0:x2}", b);
  8.     }
  9.     return sbBytes.Tostring();
  10. }

b)

  1. static string ConvertstringToHex(string input, system.Text.Encoding encoding)
  2. {
  3.     char[]string Bytes = encoding.GetBytes(input);
  4.     string Builder sbBytes = new Builder sbBytes(StringBytes.Length*2);
  5.     for each (byte 'b' in StringBytes)
  6.     {
  7.         sBytes.AppendFormat("{0:X2}", b);
  8.     }
  9.     return sbBytes.Tostring();
  10. }

c)

  1. public static string ConvertStringToHex(String input, 
  2.                                         System.Text.Encoding encoding)
  3. {
  4.     {
  5.        Byte[] stringBytes = encoding.GetBytes(input);
  6.        StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
  7.        foreach (byte b in stringBytes)
  8.        {
  9.            sbBytes.AppendFormat("{0:X2}",  b);
  10.        }
  11.    Console.WriteLine(sbBytes.ToString());//sbBytes.ToString());
  12.    return sbBytes.ToString();
  13.     }
  14. }

d) None of the mentioned

Answer: c
Explanation: None.
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 C# code is used for conversion of hex to string form?

  1. static void Main(string[] args)
  2. {
  3.     string testString = "MIKA@?&^";
  4.     string normal = ConvertHexToString (hex,  System.Text.Encoding.Unicode);
  5.     Console.WriteLine(normal);
  6.     Console.ReadLine();
  7. }

a)

  1. public static string ConvertHexToString(String hexInput,  
  2.                                         System.Text.Encoding encoding)
  3. {
  4.     char[] numberChars = hexInput.Length;
  5.     byte[] bytes = new byte[numberChars / 2];
  6.     for (int i = 0; i < numberChars; i += 2)
  7.     {
  8.         bytes[i / 2] = Convert.ToByte(hexInput.Substring(i,  0),  16);
  9.     }
  10.     return encoding.GetString(bytes);
  11. }

b)

  1. public static string ConvertHexToString(String hexInput,  
  2.                                         System.Text.Encoding encoding)
  3. {
  4.     int numberChars = hexInput.Length;
  5.     byte[] bytes = new byte[numberChars / 2];
  6.     for (int i = 0; i < numberChars; i += 2)
  7.     {
  8.         bytes[i / 2] = Convert.ToByte(hexInput.Substring(i,  2),  16);
  9.     }
  10.     return encoding.GetString(bytes);
  11. }

c)

  1. public static string ConvertHexToString(String hexInput,  
  2.                                         System.Text.Encoding encoding)
  3. {
  4.     string numberChars = hexInput.Length;
  5.     byte[] bytes = new byte[numberChars];
  6.     for (int i = 0; i &lt; numberChars; i += 2)
  7.     {
  8.         bytes[i / 2] = Convert.ToByte(hexInput.Substring(i,  2),  16);
  9.     }
  10.     return encoding.GetString(bytes);
  11. }

d) None of the mentioned

Answer: b
Explanation: None.
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 &lt; numberChars; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(hexInput.Substring(i,  2),  16);
    }
    return encoding.GetString(bytes);
}

 

12. What will be the output of the following C# code?

  1.    string s1 = " I AM BEST ";
  2.    string s2;
  3.    s2 = s1.substring (5, 4);
  4.    Console.WriteLine (s2);

a) AM BEST
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.
Output:

 

 BEST

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
Explanation: None.

 

 

 

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
Explanation: By definition.

 

 

 

 

 

15. 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.

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.97 seconds
10,846,048 unique visits