C# Questions & Answers – Properties and its Applications

This set of C# Questions and Answers for Entrance exams focuses on “Properties and its Applications”.

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

  1.    class math
  2.    {
  3.        int ad;
  4.        public int add
  5.        {
  6.            get
  7.            {
  8.                return ad;
  9.            }
  10.        }
  11.  
  12.    }

b)

advertisement
advertisement
  1.    class math
  2.    {
  3.       public int add
  4.       {
  5.           get
  6.           {
  7.               return ad;    
  8.           }
  9.       }
  10.   }

c)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.    class math
  2.    {
  3.        int ad;
  4.        public int add
  5.        {
  6.            get
  7.            {
  8.                return ad;
  9.            }
  10.            set
  11.            {
  12.                ad = value; 
  13.            }
  14.        }
  15.    }

d) None of the mentioned
View Answer

Answer: a
Explanation: None.
advertisement

2. The correct way to implement a write only property add in a math class?
a)

  1.    class math
  2.    {
  3.        public int add
  4.        {
  5.            set
  6.            {
  7.                 add = value;
  8.             }
  9.         }
  10.     }

b)

advertisement
  1.    class math
  2.    {
  3.        int ad;
  4.        public int add
  5.        {
  6.            set
  7.            {
  8.                ad = value;
  9.            }
  10.        }
  11.    }

c)

  1.   class math
  2.   {
  3.       int ad;
  4.       public int add
  5.       {
  6.           get
  7.           {
  8.               return ad;
  9.           }
  10.           set
  11.           {
  12.               ad = value;
  13.           }
  14.       }
  15.   }

d) None of the mentioned
View Answer

Answer: b
Explanation: None.

3. Select the correct statement about properties of read and write in C#.NET?
a) A property can simultaneously be read or write only
b) A property cannot be either read only or write only
c) A write only property will only have get accessor
d) A read only property will only have get accessor
View Answer

Answer: d
Explanation: None.

4. What will be the output of the following C# code?

  1.  class number
  2.  {
  3.      private int num1;
  4.      private int num2;
  5.      public int anumber
  6.      {
  7.          get
  8.          {
  9.              return num1;
  10.          }
  11.          set
  12.          {
  13.              num1 = value;
  14.          }
  15.      }
  16.      public int anumber1
  17.      {
  18.          get
  19.          {
  20.              return num2;
  21.          }
  22.          set
  23.          {
  24.              num2 = value;
  25.          }
  26.      }
  27.  }
  28.  class Program
  29.  {
  30.      public static void Main(string[] args)
  31.      {
  32.          number p = new number();
  33.          p.anumber = 20;
  34.          number k = new number();
  35.          k.anumber1 = 40; 
  36.          int m = p.anumber;
  37.          int t = k.anumber1;
  38.          int r = p.anumber + k.anumber1;
  39.          System.Console.WriteLine("number1 = " +m);
  40.          System.Console.WriteLine("number2 = " +t);
  41.          System.Console.WriteLine("sum = " +r);
  42.          System.Console.ReadLine();
  43.      }
  44.  }

a) 20
b)

number1 = 30
number2 = 40
sum = 70

c)

number1 = 20
number2 = 40
sum = 60

d) Compile time error
View Answer

Answer: c
Explanation: None.
Output :

number1 = 20
number2 = 40
sum = 60

5. What will be the output of the following C# code?

  1.   class number
  2.   {
  3.       private int num1 = 60;
  4.       private int num2 = 20;
  5.       public int anumber
  6.       {
  7.           get
  8.           {
  9.               return num1;
  10.           }
  11.           set
  12.           {
  13.               num1 = value;
  14.           }
  15.       }
  16.       public int anumber1
  17.       {
  18.           get
  19.           {
  20.               return num2;
  21.           }
  22.           set
  23.           {
  24.               num2 = value;
  25.           }
  26.       }
  27.   }
  28.   class Program
  29.   {
  30.       public static void Main(string[] args)
  31.       {
  32.           number p = new number();
  33.           number k = new number();
  34.           int m = p.anumber;
  35.           int t = k.anumber1;
  36.           int r = p.anumber * k.anumber1;
  37.           Console.WriteLine("sum = " + r);
  38.           Console.ReadLine();
  39.       }
  40.   }

a) 0
b) 120
c) 1200
d) Compile time error
View Answer

Answer: c
Explanation: None.
Output :

 1200

6. What will be the output of the following C# code?

  1.  class number
  2.  {
  3.      int length = 50;
  4.      public int number1
  5.      {
  6.          get
  7.          {
  8.              return length;
  9.          }
  10.          set
  11.          {
  12.              length = value;
  13.          }
  14.      }
  15.  }
  16.  class Program
  17.  {
  18.      public static void Main(string[] args)
  19.      {
  20.          number p = new number();
  21.          p.number1 = p.number1 + 40;
  22.          int k = p.number1 * 3 / 9;
  23.          Console.WriteLine(k);
  24.          Console.ReadLine();
  25.      }
  26.  }

a) 0
b) 180
c) 30
d) Compile time error
View Answer

Answer: c
Explanation: None.
Output :

30

7. What will be the output of the following C# code?

  1.   class number
  2.   {
  3.       int length = 60;
  4.       public int number1
  5.       {
  6.           get
  7.           {
  8.               return length;
  9.           }
  10.       }
  11.   }
  12.   class Program
  13.   {
  14.       public static void Main(string[] args)
  15.       {
  16.           number p = new number();
  17.           int l;
  18.           l = p.number1 + 40;
  19.           int k = l * 3 / 4;
  20.           Console.WriteLine(k);
  21.           Console.ReadLine();
  22.       }
  23.   }

a) 30
b) 75
c) 80
d) 0
View Answer

Answer: b
Explanation: None.
Output :

75

8. What will be the output of the following C# code?

  1.   class student
  2.   {
  3.       int []scores = new int[5] {23, 42, 54, 11, 65};
  4.       public int this[int index]
  5.       {
  6.           get
  7.           {
  8.               if (index < 5)
  9.               return scores[index];
  10.               else
  11.               {
  12.                   Console.WriteLine("invalid index");
  13.                   return 0;
  14.               }
  15.           }
  16.           set
  17.           {
  18.               if (index < 5)
  19.               scores[index] = value;
  20.               else
  21.               Console.WriteLine("invalid index");
  22.           }
  23.       }
  24.   }
  25.   class Program
  26.   {
  27.       public static void Main(string[] args)
  28.       {
  29.           student s = new student();
  30.           Console.WriteLine(s[4] + 8);
  31.           Console.ReadLine();
  32.       }
  33.   }

a) 73
b) 37
c) 0
d) Run time error
View Answer

Answer: a
Explanation: None.
Output :

73

9. 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 integer arrays.
a)

  1.  class student
  2.  {
  3.      int []scores = new int[5] {23, 42, 54, 11, 65};
  4.      public int this[int index]
  5.      {
  6.          get
  7.          {
  8.              if (index < 5)
  9.              return scores[index];
  10.              else
  11.              {
  12.                  Console.WriteLine("invalid index");
  13.                  return 0;
  14.              }
  15.          }
  16.          set
  17.          {
  18.              if (index < 5)
  19.              scores[index] = value;
  20.              else
  21.              Console.WriteLine("invalid index");
  22.          }
  23.      }
  24.  }

b)

  1.  class student
  2.  {
  3.      int []scores = new int[5] {23, 42, 54, 11, 65};
  4.      public int this[int index]
  5.      {
  6.          get
  7.          {
  8.              if (index < 5)
  9.              return scores[index];
  10.          }
  11.      }
  12.  }

c)

  1.  class student
  2.  {
  3.      int []scores = new int[5]{23, 42, 54, 11, 65};
  4.      public int this[int index]
  5.      {
  6.          set
  7.          {
  8.              if (index < 5)
  9.              return scores[index];
  10.              else
  11.              {
  12.                  Console.WriteLine("invalid index");
  13.                  return 0;
  14.              }
  15.          }
  16.      }
  17.  }

d) None of the mentioned
View Answer

Answer: a
Explanation: None.

10. What will be the output of the following C# code?

  1.  class student
  2.  {
  3.      int []scores = new int[3] {13, 32, 24};
  4.      public int this[int index]
  5.      {
  6.          get
  7.          {
  8.              if (index < 3)
  9.              return scores[index];
  10.              else
  11.              {
  12.                  Console.WriteLine("invalid index");
  13.                  return 0;
  14.              }
  15.          }
  16.          private  set
  17.          {
  18.              if (index < 3)
  19.              scores[index] = value;
  20.              else
  21.              Console.WriteLine("invalid index");
  22.          }
  23.      }
  24.  }
  25.  class Program
  26.  {
  27.      public static void Main(string[] args)
  28.      {
  29.          student s = new student();
  30.          int[] scores1 = new int[3] {8, 19, 40};
  31.          for (int i = 0; i < 3; i++)
  32.          {
  33.              if (scores1[i] > s[i])
  34.              {
  35.                  Console.WriteLine(" scores1 had greater value :" + scores1[i]);
  36.              }
  37.              else
  38.              {
  39.                  Console.WriteLine("scores had greater value :" + s[i]);
  40.              }
  41.          }
  42.          Console.ReadLine();
  43.      }
  44.  }

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
View Answer
Answer: d
Explanation: None.
Output :

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

Sanfoundry Global Education & Learning Series – C# Programming Language.
Here’s the list of Best Books in C# Programming Language.

To practice all areas of C# for Entrance exams, here is complete set on 1000+ Multiple Choice Questions and Answers on C#

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.