C# Questions & Answers – Operation on Characters

This section of our 1000+ C# multiple choice questions focuses on operation of characters in C# Programming Language.

1. Which of these methods of the class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()
View Answer

Answer: d
Explanation: Method length() of string class is used to get the length of the object as string. Length and hence invokes the length() method.

2. Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?
a) getBytes()
b) GetByte()
c) giveByte()
d) Give Bytes()
View Answer

Answer: a
Explanation: getBytes() stores the character in an array of bytes. It uses default character to byte conversions.

3. Which of these methods can be used to convert all characters in a String into a character array?
a) CharAt()
b) getChars()
c) TocharArray()
d) All of the mentioned
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

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

  1.  static void main(String args[])
  2.  {
  3.      char chars[] = {'x', 'y', 'z'};
  4.      String s = new String(chars);
  5.      Console.WriteLine(s);
  6.  }

a) x
b) xy
c) z
d) xyz
View Answer

Answer: d
Explanation: String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “xyz”.
Output :

xyz

5. Choose the effective stringBuilder method which helps in producing output for the following C# code?

advertisement
  1.  static void Main(string[] args)
  2.  {
  3.      StringBuilder s = new StringBuilder("object");
  4.      s./*______*/("Oriented Language");
  5.      Console.WriteLine(s);
  6.      Console.ReadLine();
  7.  }
  8. Output : objectOriented Language

a) Insert()
b) Add()
c) Append()
d) Join()
View Answer

Answer: c
Explanation:

advertisement
              static void Main(string[] args)
              {
               StringBuilder s = new StringBuilder("object");
               s.Append("Oriented Language");
               Console.WriteLine(s);
               Console.ReadLine();
               }
Output : objectOriented Language

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

  1.   static void Main(string[] args)
  2.   {
  3.       string s = " i love you";
  4.       Console.WriteLine(s.IndexOf('l') + "  " + s.lastIndexOf('o') + "  " + s.IndexOf('e'));
  5.       Console.ReadLine();
  6.   }

a) 3 5 7
b) 4 5 6
c) 3 9 6
d) 2 4 6
View Answer

Answer: c
Explanation: indexof(‘l’) and lastIndexof(‘o’) are pre defined functions which are used to get the index of first and last occurrence of the character pointed by l and c respectively in the given array.
Output :

 3, 9, 6

7. Which of these methods of class String is used to extract all the characters from a String object?
a) CHARAT()
b) Remove()
c) charAt()
d) Replace()
View Answer

Answer: b
Explanation: Replace() replaces all instances of a character with a new character while Remove extracts characters from the string.

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

  1.  static void Main(string[] args)
  2.  {
  3.      string c = "hello";
  4.      string  c1 = c.Remove(1);
  5.      Console.WriteLine(c1);
  6.      Console.ReadLine();
  7.  }

a) ello
b) h
c) hell
d) none of the mentioned
View Answer

Answer: b
Explanation: The remove() deletes characters from the string except the character which is specified with its given position.
Output :

h

9. How is a string typically processed?
a) On a character by character basis
b) On a string by string basis
c) Both On a character by character basis & On a string by string basis
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

10. How to print \\ on the screen?
a) Console.WriteLine(“\\”);
b) Console.WriteLine(“\\\”);
c) Console.WriteLine(“\\\\”);
d) Console.WriteLine(“\\\\\\”);
View Answer

Answer: c
Explanation: Console.WriteLine(“\\\\”);
Output :

 \\

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.