250+ TOP MCQs on Operation on Characters and Answers

C# multiple choice questions 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()

Answer: d
Clarification: 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()

Answer: a
Clarification: 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

Answer: c
Clarification: None.

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

Answer: d
Clarification: 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 :

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

  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()

Answer: c
Clarification:

              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

Answer: c
Clarification: 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 :

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()

Answer: b
Clarification: 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

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

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

Answer: a
Clarification: None.

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *