Examveda

What will be the output of the following C# code?
class student
{
    int []scores = new int[3] {13, 32, 24};
    public int this[int index]
    {
        get
        {
            if (index < 3)
            return scores[index];
            else
            {
                Console.WriteLine("invalid index");
                return 0;
            }
        }
        private  set
        {
            if (index < 3)
            scores[index] = value;
            else
            Console.WriteLine("invalid index");
        }
    }
}
class Program
{
    public static void Main(string[] args)
    {
        student s = new student();
        int[] scores1 = new int[3] {8, 19, 40};
        for (int i = 0; i < 3; i++)
        {
            if (scores1[i] > s[i])
            {
                Console.WriteLine(" scores1 had greater value :" + scores1[i]);
            }
            else
            {
                Console.WriteLine("scores had greater value :" + s[i]);
            }
        }
        Console.ReadLine();
    }
}

B. Compile time error

C. Run time error

D. scores had greater value : 13
scores had greater value : 32
scores1 had greater value : 40

Answer: Option D


Join The Discussion

Related Questions on Exception Handling in C Sharp