250+ TOP MCQs on Searching and Modifying Strings and Answers

C# multiple choice questions on searching and modification of strings in C# Programming Language.

1. Which of these methods of class String is used to separate a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned

Answer: b
Clarification: None.

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

  1.  static void Main(string[] args)
  2.  {
  3.      String a = "Ilove";
  4.      String b = "CSHARP";
  5.      b = string.Concat(a, '  ', b);
  6.      Console.WriteLine(b);
  7.      Console.ReadLine();
  8.  }

a) IloveCSHARP
b) I loveCSHARP
c) Ilove
d) Ilove CSHARP

Answer: d
Clarification: Concat() method is used to join two strings without the use of ‘+’ operator.
Output :

3. Which of these methods of class are used to remove the leading and backward whitespaces?
a) startsWith()
b) trim()
c) Trim()
d) doTrim()

Answer: c
Clarification: None.

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

  1.   static void Main(string[] args)
  2.   {
  3.       String a = "Ilove";
  4.       String b = "CSHARP";
  5.       b = string.Concat(a,' ',b);
  6.       string d = b.TrimStart('I', 'l', 'o', 'H');
  7.       Console.WriteLine(d);
  8.       Console.ReadLine();
  9.   }

a) Ilove CSHARP
b) love CSHARP
c) ve CSHARP
d) ve CSARP

Answer: c
Clarification: trimstart() removes character mentioned consecutively in front positions not characters in mentioned in between positions.
Output :

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

  1.  static void Main(string[] args)
  2.  {
  3.      String c = "  Hello Computer ";
  4.      String a = c.Trim();
  5.      Console.WriteLine(""" + s + """);
  6.  }

a) ” Hello Computer ”
b) “HelloComputer”
c) “Hello Computer”
d) Hello Computer

Answer: c
Clarification: Trim() method is used to remove forward and backward spaces in strings.
Output :

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

  1.  static void Main(string[] args)
  2.  {
  3.      String c = "Hello";
  4.      String a = c + "Bye";
  5.      Console.WriteLine(a);
  6.      Console.ReadLine();
  7.  }

a) “Hello Bye”
b) “HelloBye”
c) Hello Bye
d) HelloBye

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

  1. static void Main(string[] args)
  2. {
  3.     String c = "Hello";
  4.     String a ;
  5.     a = c.Replace('l', 'w');
  6.     Console.WriteLine(a);
  7.     Console.ReadLine();
  8. }

a) Helloll
b) Hewlo
c) Helwo
d) Hewwo

Answer: d
Clarification: None.
Output :

8. Which of the following statements is correct?
a) replace() replace() method replaces last occurrence of a character in invoking strings with another character
b) replace() method replaces only first occurrence of a character in invoking strings with another character
c) replace() method replaces all occurrences of one character in invoking strings with another character
d) none of the mentioned

Answer: c
Clarification: By definition.

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

  1.   static void Main(string[] args)
  2.   {
  3.       String c = "Hello i love you";
  4.       String a ;
  5.       a = c.Substring(12, 3);
  6.       Console.WriteLine(a);
  7.       Console.ReadLine();
  8.   }

a) ove
b) you
c) yo
d) love you

Answer: c
Clarification: None.
Output :

10. Which among the following is the correct way to find out the index of second ‘s’ in the string “She sold her beauty in one night to someone else”?
a)

   String a = "She sold her beauty in one night to someone else";
   int i;
   i = a.SecondIndexOf("s");

b)

   String a = "She sold her beauty in one night to someone else";
   int i, j;
   i = a.FirstIndexOf("s");
   j = a.IndexOf("s", i + 1);

c)

   String a = "She sold her beauty in one night to someone else";
   int i, j;
   i = a.IndexOf("s");
   j = a.IndexOf("s", i + 1);

d) None of the mentioned

Answer: c
Clarification:

       static void Main(string[] args)
        {
            String c = "She sold her beauty in one night to someone else";
            int i,j;
 
            i = c.IndexOf("s");
            j = c.IndexOf("s", i + 1);
            Console.WriteLine(i + " " + j);
           Console.ReadLine();
        }
Output : 4, 36

Leave a Comment

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

Scroll to Top