What will be the output of the following C# code snippet? [code] class Program { static void Main(string[] args) { String

What will be the output of the following C# code snippet?
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 a value greater than zero if the invoking string is greater than the string compared to 4
Output:
Code:
zy
 

Tìm kiếm

Back
Top