adplus-dvertising
frame-decoration

Question

What will be the output of the following snippet of 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();
     }
 }

a.

0

b.

Compile time error

c.

Run time error

d.

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

Posted under C# programming

Answer: (d).scores had greater value : 13
scores had greater value : 32
scores1 had greater value : 40

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What will be the output of the following snippet of code?

Similar Questions

Discover Related MCQs

Q. Which among the following is NOT an exception?

Q. Which among the following is considered as .NET Exception class?

Q. Which of the following is the object oriented way to handle run time errors?

Q. Select the statements which describe the correct usage of exception handling over conventional error handling approaches?

Q. Select the correct statement about an Exception?

Q. Which of these keywords is not a part of exception handling?

Q. Which of these keywords must be used to monitor exceptions?

Q. Which of these keywords is used to manually throw an exception?

Q. Which of the following is the wrong statement about exception handling in C#.NET?

Q. When no exception is thrown at runtime then who will catch it?

Q. Choose the correct statement which makes exception handling work in C#.NET?

Q. Which of these clauses will be executed even if no exceptions are found?

Q. A single try block must be followed by which of these?

Q. Which of these exceptions handles the divide by zero error?

Q. Which of these exceptions will occur if we try to access the index of an array beyond its length?

Q. Which of the following keywords is used by the calling function to guard against the exception that is thrown by called function?

Q. Which of these classes is related to all the exceptions that are explicitly thrown?

Q. What is the use of try & catch?

Q. Choose the statement which is incorrect?

Q. Which of the keywords are used for the block to be examined for exceptions?