C# Questions & Answers – Comparison of Strings

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

1. Which of these methods of class String is used to compare two String objects for their equality?
a) equals()
b) Equals()
c) isequal()
d) Isequal()
View Answer

Answer: a
Explanation: None.

2. Which of these methods is used to compare two strings such that after comparison output returns different integer values as (0 for false, 1 for true)?
a) Equals ()
b) == operator
c) Compare()
d) None of the mentioned
View Answer

Answer: c
Explanation: The comparison is case sensitive in nature and hence different integer values are returned for different conditions as under:
1. zero integer (0), if string s1 equal to string s2.
2. positive integer(+1), if string s1 greater than s2.
3. Negative integer(-1), if string s1 is less than s2.

3. Which of these methods of class String is used to check whether a substring exists at the beginning of the particular string?
a) StartsWith()
b) EndsWith()
c) Starts()
d) ends()
View Answer

Answer: a
Explanation: Method startswith() of string class is used to check whether a substring exists in the beginning of string or not.
advertisement
advertisement

4. Which of these methods returns the string such that some characters which are specified to be removed from the end of strings are removed from string by mentioning the number of characters to be removed?
a) Trim()
b) Remove()
c) TrimEnd()
d) Split()
View Answer

Answer: a
Explanation: Removes a string of characters from the end of string by mentioning the number of characters to be removed from the string.

5. What is the value returned by function compareTo() if the invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) none of the mentioned
View Answer

Answer: b
Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of these data type values is returned by equals() method of String class?
a) char
b) int
c) boolean
d) all of the mentioned
View Answer

Answer: c
Explanation: equals() method of string class returns boolean value true if both the strings are equal and false if they are unequal.

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

advertisement
  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      { 
  5.          String c = "i love Csharp";
  6.          bool a;
  7.          a = c.StartsWith("I");
  8.          Console.WriteLine(a);
  9.          Console.ReadLine();
  10.      }
  11.  }

a) true
b) false
c) 0
d) 1
View Answer

Answer: b
Explanation: StartsWith() method is case sensitive “i” and “I” are treated differently, hence false is stored in a.
Output:

advertisement
false

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     { 
  5.         String s1 = "I love You";
  6.         String s2 = s1;
  7.         Console.WriteLine((s1 == s2) + " " + s1.Equals(s2));
  8.         Console.ReadLine();
  9.     }
  10. }

a) true true
b) false false
c) true false
d) false true
View Answer

Answer: a
Explanation: The ‘==’ operator tests the equality of strings and since s1 = “I love You” and also s2 = s1. So, true is returned. Similarly, Equals() returns true.
since the content of both s1 and s2 are equal in nature.
Output :

true true

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      { 
  5.          String []chars = {"z", "x", "y", "z", "y"};
  6.          for (int i = 0; i < chars.Length; ++i)
  7.          for (int j = i + 1; j < chars.Length; ++j)
  8.          if(chars[i].CompareTo(chars[j]) == 0)
  9.          Console.WriteLine(chars[j]);
  10.          Console.ReadLine();
  11.      }
  12.  }

a) zx
b) xy
c) zy
d) yz
View Answer

Answer: c
Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared To 4.
Output :

zy

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

  1. String a = "Csharp";
  2. String b = "CSHARP";
  3. int c;
  4. c = a.CompareTo(b);
  5. Console.WriteLine(c);

a) 0
b) 1
c) -2
d) -1
View Answer

Answer: d
Explanation: Negative integer -1 is returned as ‘a’ is less than ‘b’ by CompareTo() method.
Output :

-1

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.