C# Programming Questions and Answers Part-17

1. To perform comparison operation on strings supported operations are ____________
a) Compare()
b) Equals()
c) Assignment ‘==’ operator
d) All of the mentioned

Answer: d
Explanation: All of the mentioned

2. What will be the output of the following C# code?
static void Main(string[] args)
{
string s1 = "Hello I Love Csharp ";
Console.WriteLine(Convert.ToChar( (s1.IndexOf('I') - s1.IndexOf('l')) * s1.IndexOf('p'));
Console.ReadLine();
}
a) I
b) Hello I
c) Love
d) H

Answer: d
Explanation: ‘I’ = index position[6], ‘l’ = index position[2]. So, I – l = 6-2 = 4*(index position of p = 18) = 72. Character with ASCII Value 72 = ‘H’.

3. Correct way to convert a string to uppercase using string class method()?
a) Upper()
b) ToUpper()
c) Object.ToUpper()
d) None of the mentioned

Answer: c
Explanation: string s1 = “Hello I Love Csharp “;
Console.WriteLine(s1.ToUpper());
Output: HELLO I LOVE CSHARP.

4. What will be the output of the following C# code?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
Console.WriteLine (obj.Equals(obj2) + " " + obj2.CompareTo(obj) );
Console.ReadLine();
}
a) True True
b) False False
c) True 0
d) False 1

Answer: c
Explanation: Equal() checks if two string objects ‘obj’ and ‘obj2’ are equal or not and hence returns true or false. Similarly, “CompareTo” operator check two objects and since string obj2 = obj, it returns bool value ‘0’. Hence, they are equal.
Output :
True 0

5. What will be the output of the following C# code?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
Console.WriteLine(obj + " " + obj1);
string s = obj + " " + obj1;
Console.WriteLine(s.Length);
Console.ReadLine();
}
a) hello world
10
b) hello world
6
c) hello world
11
d) hello world
5

Answer: c
Explanation: Length() method calculates number of characters in a string. ‘Obj2’ assumes the value of object ‘obj’ in itself.
Output:
hello world
11

6. What will be the output of the following C# code?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
string s = obj+" "+obj1;
Console.WriteLine(s.IndexOf('r'));
Console.ReadLine();
}
a) 7
b) 8
c) 9
d) 10

Answer: b
Explanation: IndexOf() used to find absolute position of a character of substring.
Output:
8

7. What will be the output of the following C# code?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
string s = obj + " " + obj1;
Console.WriteLine(s.Substring(6 ,5));
Console.ReadLine();
}
a) hello
b) orld
c) world
d) o world

Answer: c
Explanation: ‘Substring()’ extract substrings from a given string using overloaded substring() method provided by string class.
Output:
world

8. What will be the output of the following C# code?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "worn";
String obj2 = obj;
Console.WriteLine(obj + " " + (obj1.Replace('w' ,'c')));
Console.ReadLine();
}
a) hello hello
b) hello worn
c) hello corn
d) hello

Answer: c
Explanation: Replace() method provided by string builder class is used to replace characters.
Output:
hello corn

9. 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
Explanation: equals()

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