C# Questions & Answers – Basic Operation on Strings

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

1. Which of the following string() method are used to compare two strings with each other?
a) CopyTo()
b) Copy()
c) Compare()
d) CompareTo()
View Answer

Answer: d
Explanation: In C#, the CompareTo() method is used to compare two strings and determine their relationship. It returns 0 if the strings are equal, a negative value if the first string is less, and a positive value if the first string is greater.

2. Choose the base class for string() method:
a) System.Array
b) System.char
c) System.String
d) None of the mentioned
View Answer

Answer: c
Explanation: String is an alias for the predefined “System.string” class from which most of the string() methods are derived.

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

advertisement
advertisement
  1.  static void Main(string[] args)
  2.  {
  3.      string s1 = " Cshr ";
  4.      string s2 = s1.Insert(3 , " a ");
  5.      string s3 = s2.Insert(5 , " p ");
  6.      for (int i = 0;i < s3.Length; i++)
  7.      Console.WriteLine(s3[i]);
  8.      Console.ReadLine();
  9.  }

a) Cshar
b) CsharP
c) Csharp
d) Cshrap
View Answer

Answer: c
Explanation: Insertion of character ‘a’ at position ‘3’ using insert() which returns a new string with a substring inserted at a specified location.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
 Csharp

4. Which of the following statement is correct about a string in C#.NET?
a) The System.Array class is used to represent a string
b) A string has a zero-based index
c) A number cannot be represented in the form of a string
d) A string is mutable because it can be modified once it has been created
View Answer

Answer: b
Explanation: None.
advertisement

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

  1.  static void Main(string[] args)
  2.  {
  3.      string s1 = "Hello";
  4.      string s2 = "hello";
  5.      if (s1 == s2)
  6.      Console.WriteLine("Equal");
  7.      else
  8.      Console.WriteLine("Unequal");
  9.      if (s1.Equals (s2))
  10.      Console.WriteLine("Equal");
  11.      else
  12.      Console.WriteLine("Unequal");
  13.      Console.ReadLine();
  14.  }

a)

Equal
Unequal
advertisement

b)

Unequal
Equal

c)

Equal
Equal

d)

Unequal
Unequal
View Answer
Answer: d
Explanation: In first comparison it is being checked either two strings are equal or not but in second comparison it is checked whether two references are equal or not.
Output:

        Unequal
        Unequal
 
 

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

  1. static void Main(string[] args)
  2. {
  3.     string s1 = "Hello" + " I " + "Love" + " ComputerScience ";
  4.     Console.WriteLine(s1);
  5.     Console.ReadLine();
  6. }

a) HelloILoveComputerScience
b) Hello I Love ComputerScience
c) Compile time error
d) Hello
View Answer

Answer: b
Explanation: Here ‘+’ defined operator works as concatenation for strings.
Output :

 Hello I Love ComputerScience

7. Correct way to find if contents of two strings are equal?
a) if (s1 = s2)
b) if (s1 != s2)
c) if (strcmp (s1 ,s2))
d) if ( s1 is s2)
View Answer

Answer: c
Explanation: “==” operator used to compare length of two strings and strcmp() is the inbuilt method derived from string class.

8. Which of the following statements are correct?
a) String is value type
b) String literals can contain any character literal including escape sequences
c) The equality operators are defined to compare values of string objects as well as references
d) All of the mentioned
View Answer

Answer: b
Explanation: None

9. Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||
View Answer

Answer: a
Explanation:

             string s1 = "Hello"+ " I " + "Love" + " ComputerScience ";
             Console.WriteLine(s1);
             Hello I Love ComputerScience.

10. The Method use to remove white space from a string?
a) Split()
b) Substring()
c) Trim()
d) TrimStart()
View Answer

Answer: c
Explanation: Perfectly removes whitespace from string whereas TrimStart() removes a string of characters from the end of the string.

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.