Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

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();
}

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

Join The Discussion