What is the value returned by the function compareTo () If the invoking string is less than the string compared?

C# multiple choice questions 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()
Answer: a
Clarification: 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
Answer: c
Clarification: 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()
Answer: a
Clarification: Method startswith() of string class is used to check whether a substring exists in the beginning of string or not.

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()
Answer: a
Clarification: 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
Answer: b
Clarification: 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.

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

  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
Answer: b
Clarification: StartsWith() method is case sensitive “i” and “I” are treated differently, hence false is stored in a.
Output:

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

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

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
Answer: d
Clarification: Negative integer -1 is returned as ‘a’ is less than ‘b’ by CompareTo() method.
Output :

What is the value returned by function compareTo () If the invoking string is greater than the string compared *?

4. What is the value returned by function compareTo() if the invoking string is greater than the string compared? Explanation: if (s1 == s2) then 0, if(s1 &gt; s2) &gt; 0, if (s1 &lt; s2) then &lt; 0.

What is the return type of string compareTo () method?

The Java String class compareTo() method compares the given string with the current string lexicographically. It returns a positive number, negative number, or 0. It compares strings on the basis of the Unicode value of each character in the strings.

Which data type value is returned by equals () method of string class?

Which of these data type values is returned by equals() method of String class? Explanation: equals() method of string class returns boolean value true if both the strings are equal and false if they are unequal.

Which of these method of class string is used to compare two string objects for their equality equals () equals () Isequal () Isequal ()?

3) By Using compareTo() method The String class compareTo() method compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string. Suppose s1 and s2 are two String objects. If: s1 == s2 : The method returns 0.