What will be the output of the following C# code?
class student
{
int []scores = new int[5] {23, 42, 54, 11, 65};
public int this[int index]
{
get
{
if (index < 5)
return scores[index];
else
{
Console.WriteLine("invalid index");
return 0;
}
}
set
{
if (index < 5)
scores[index] = value;
else
Console.WriteLine("invalid index");
}
}
}
class Program
{
public static void Main(string[] args)
{
student s = new student();
Console.WriteLine(s[4] + 8);
Console.ReadLine();
}
}
class student
{
int []scores = new int[5] {23, 42, 54, 11, 65};
public int this[int index]
{
get
{
if (index < 5)
return scores[index];
else
{
Console.WriteLine("invalid index");
return 0;
}
}
set
{
if (index < 5)
scores[index] = value;
else
Console.WriteLine("invalid index");
}
}
}
class Program
{
public static void Main(string[] args)
{
student s = new student();
Console.WriteLine(s[4] + 8);
Console.ReadLine();
}
}A. 73
B. 37
C. 0
D. Run time error
Answer: Option A
Related Questions on Exception Handling in C Sharp
What is the purpose of exception handling in C#?
A. To enhance program performance
B. To avoid writing error messages
C. To gracefully handle runtime errors
D. To simplify debugging
What is the default exception handler in C# called?
A. Standard Exception Handler
B. Unhandled Exception Handler
C. Global Exception Handler
D. None of the above
A. throw
B. catch
C. finally
D. try

Join The Discussion