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? 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)? 3. Which of these methods of class String is used to check whether a substring exists at the beginning of the particular string? 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? 5. What is the value returned by function compareTo() if the invoking string is less than the string compared?
a) equals()
b)
Equals()
c) isequal()
d) Isequal()
Answer: a
Clarification: None.
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.
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.
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.
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?
- class Program
- {
- static void Main(string[] args)
- {
- String c = "i love Csharp";
- bool a;
- a = c.StartsWith("I");
- Console.WriteLine(a);
- Console.ReadLine();
- }
- }
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?
- class Program
- {
- static void Main(string[] args)
- {
- String s1 = "I love You";
- String s2 = s1;
- Console.WriteLine((s1 == s2) + " " + s1.Equals(s2));
- Console.ReadLine();
- }
- }
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?
- class Program
- {
- static void Main(string[] args)
- {
- String []chars = {"z", "x", "y", "z", "y"};
- for (int i = 0; i < chars.Length; ++i)
- for (int j = i + 1; j < chars.Length; ++j)
- if(chars[i].CompareTo(chars[j]) == 0)
- Console.WriteLine(chars[j]);
- Console.ReadLine();
- }
- }
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?
- String a = "Csharp";
- String b = "CSHARP";
- int c;
- c = a.CompareTo(b);
- 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 :