C# Questions & Answers – Searching and Modifying Strings

This section of our 1000+ C# multiple choice questions focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

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

advertisement
advertisement
Ilove CSHARP

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

Answer: c
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
  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
View Answer

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

advertisement
 ve CSHARP

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
View Answer

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

 "Hello Computer"

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
View Answer

Answer: d
Explanation: ‘+’ operator method works in the form of concatenate method() and hence is used to join two strings together.
Output :

 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
View Answer

Answer: d
Explanation: None.
Output :

Hewwo

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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: c
Explanation: None.
Output :

yo

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
View Answer

Answer: c
Explanation:

       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

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.