C# Questions & Answers on Comparison of Strings for Freshers

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

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

Explanation: 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

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
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.
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

Explanation: equals() method of string class returns boolean value true if both the strings are equal and false if they are unequal.
7. What is output of the given set of 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

Explanation: StartsWith() method is case sensitive “i” and “I” are treated differently, hence false is stored in a.
Output: false
8. What is the output of the given set of 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

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 is output of the given set of 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

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 : z
y
10. What will be the output for the given set of 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

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

Related

Multiple Choice Questions 6251743917977924538

Post a Comment

emo-but-icon

item