adplus-dvertising

Welcome to the Indexers and Exception Handling MCQs Page

Dive deep into the fascinating world of Indexers and Exception Handling with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Indexers and Exception Handling, a crucial aspect of C# Programming. In this section, you will encounter a diverse range of MCQs that cover various aspects of Indexers and Exception Handling, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within C# Programming.

frame-decoration

Check out the MCQs below to embark on an enriching journey through Indexers and Exception Handling. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of C# Programming.

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Indexers and Exception Handling. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Indexers and Exception Handling MCQs | Page 8 of 13

Explore more Topics under C# Programming

Q71.
The correct way to implement a read only property add, in a math class is?

   class math
   {
       int ad;
       public int add
       {
           get
           {
               return ad;
           }
       }
 
   }

b)

   class math
   {
      public int add
      {
          get
          {
              return ad;    
          }
      }
  }

c)

   class math
   {
       int ad;
       public int add
       {
           get
           {
               return ad;
           }
           set
           {
               ad = value; 
           }
       }
   }
Discuss
Answer: (a).a
Q72.
The correct way to implement a write only property add in a math class?
a)

   class math
   {
       public int add
       {
           set
           {
                add = value;
            }
        }
    }

b)

   class math
   {
       int ad;
       public int add
       {
           set
           {
               ad = value;
           }
       }
   }

c)

  class math
  {
      int ad;
      public int add
      {
          get
          {
              return ad;
          }
          set
          {
              ad = value;
          }
      }
  }
Discuss
Answer: (b).b
Discuss
Answer: (d).A read only property will only have get accessor
Q74.
What will be the output of following snippet of code?
class number
 {
     private int num1;
     private int num2;
     public int anumber
     {
         get
         {
             return num1;
         }
         set
         {
             num1 = value;
         }
     }
     public int anumber1
     {
         get
         {
             return num2;
         }
         set
         {
             num2 = value;
         }
     }
 }
 class Program
 {
     public static void Main(string[] args)
     {
         number p = new number();
         p.anumber = 20;
         number k = new number();
         k.anumber1 = 40; 
         int m = p.anumber;
         int t = k.anumber1;
         int r = p.anumber + k.anumber1;
         Console.WriteLine("number = " +m);
         Console.WriteLine("number = " +t);
         Console.WriteLine("sum = " +r);
         Console.ReadLine();
     }
 }
Discuss
Answer: (c).60
Q75.
What will be the output of the following snippet of code?
class number
  {
      private int num1 = 60;
      private int num2 = 20;
      public int anumber
      {
          get
          {
              return num1;
          }
          set
          {
              num1 = value;
          }
      }
      public int anumber1
      {
          get
          {
              return num2;
          }
          set
          {
              num2 = value;
          }
      }
  }
  class Program
  {
      public static void Main(string[] args)
      {
          number p = new number();
          number k = new number();
          int m = p.anumber;
          int t = k.anumber1;
          int r = p.anumber * k.anumber1;
          Console.WriteLine("sum = " + r);
          Console.ReadLine();
      }
  }
Discuss
Answer: (c).1200
Q76.
What will be the output of the following snippet of code?
class number
 {
     int length = 50;
     public int number1
     {
         get
         {
             return length;
         }
         set
         {
             length = value;
         }
     }
 }
 class Program
 {
     public static void Main(string[] args)
     {
         number p = new number();
         p.number1 = p.number1 + 40;
         int k = p.number1 * 3 / 9;
         Console.WriteLine(k);
         Console.ReadLine();
     }
 }
Discuss
Answer: (c).30
Q77.
What will be the output of the following snippet of code?
class number
  {
      int length = 60;
      public int number1
      {
          get
          {
              return length;
          }
      }
  }
  class Program
  {
      public static void Main(string[] args)
      {
          number p = new number();
          int l;
          l = p.number1 + 40;
          int k = l * 3 / 4;
          Console.WriteLine(k);
          Console.ReadLine();
      }
  }
Discuss
Answer: (b).75
Q78.
What will be the output of the following snippet of 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();
      }
  }
Discuss
Answer: (a).73
Q79.
The correct way to implement the property for which property reports the error β€œinvalid index” if user attempts to cross bounds of the array for a student class with 5 intger arrays.
a)

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

b)

 class student
 {
     int []scores = new int[5] {23, 42, 54, 11, 65};
     public int this[int index]
     {
         get
         {
             if (index < 5)
             return scores[index];
         }
     }
 }

c)

 class student
 {
     int []scores = new int[5]{23, 42, 54, 11, 65};
     public int this[int index]
     {
         set
         {
             if (index < 5)
             return scores[index];
             else
             {
                 Console.WriteLine("invalid index");
                 return 0;
             }
         }
     }
 }
Discuss
Answer: (a).a
Q80.
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();
     }
 }
Discuss
Answer: (d).scores had greater value : 13
scores had greater value : 32
scores1 had greater value : 40

Suggested Topics

Are you eager to expand your knowledge beyond C# Programming? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!