C# Programming Questions and Answers Part-18

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

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

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

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

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

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

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

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

9. Which of these methods of the class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()

Answer: d
Explanation: Method length() of string class is used to get the length of the object as string. Length and hence invokes the length() method.

10. Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?
a) getBytes()
b) GetByte()
c) giveByte()
d) Give Bytes()

Answer: a
Explanation: getBytes() stores the character in an array of bytes. It uses default character to byte conversions.