250+ TOP MCQs on Accessor controls of class and Answers

This set of C# Assessment Questions and Answers on “Accessor controls of class”.

1. Which among these access specifiers should be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned

Answer: b
Clarification: main() method must be specified public as it called by Csharp run time system outside of the program, by default main is private in nature if no access specifier is used.

2. Which of these is used as default for a member of a class if no access specifier is used for it?
a) private
b) public
c) protected internal
d) protected

Answer: a
Clarification: None.

3. What is the process by which we can control what parts of a program can access the members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion

Answer: c
Clarification: None.

4. Which of these base class are accessible to the derived class members?
a) static
b) protected
c) private
d) shared

Answer: b
Clarification: None.

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

  1.  class access
  2.  {
  3.      public int x;
  4.      private int y;
  5.      public  void cal(int a, int b)
  6.      {
  7.          x = a + 1;
  8.          y = b;
  9.      }
  10.  }
  11.  class Program
  12.  {
  13.      static void Main(string[] args)
  14.      {
  15.          access obj = new access();
  16.          obj.cal(2, 3);
  17.          Console.WriteLine(obj.x + " " + obj.y);
  18.      }
  19.  }

a) 3 3
b) 2 3
c) Run time error
d) Compile time error

Answer: d
Clarification: ‘y’ is defined privately which cannot be accessed outside its scope.

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

  1. class access
  2. {
  3.     public int x;
  4.     private int y;
  5.     public  void cal(int a, int b)
  6.     {
  7.         x = a + 1;
  8.         y = b;
  9.     }
  10.     public  void print()
  11.    {
  12.        Console.WriteLine(" " + y);
  13.    }
  14. }
  15. class Program
  16. {
  17.     static void Main(string[] args)
  18.     {
  19.         access obj = new access();
  20.         obj.cal(2, 3);
  21.         Console.WriteLine(obj.x);
  22.         obj.print();
  23.         Console.ReadLine();
  24.     }
  25. }

a) 2 3
b) 3 3
c) Run time error
d) Compile time error

Answer: b
Clarification: None.

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

  1.  class sum
  2.  {
  3.      public int x;
  4.      public int y;
  5.      public  int add (int a, int b)
  6.     {
  7.         x = a + b;
  8.         y = x + b;
  9.         return 0;
  10.     }
  11. }
  12. class Program
  13. {
  14.     static void Main(string[] args)
  15.     {
  16.         sum obj1 = new sum();
  17.         sum obj2 = new sum();
  18.         int a = 2;
  19.         obj1.add(a, a + 1);
  20.         obj2.add(5, a);
  21.         Console.WriteLine(obj1.x + "  " + obj2.y);
  22.         Console.ReadLine();
  23.     }
  24. }

a) 6, 9
b) 5, 9
c) 9, 10
d) 3, 2

Answer: b
Clarification: Here, a = 2, a + 1 = 2 + 1 = 3.
So, a = 2, b = 3.
x = 2 + 3 = 5.
y = 5 + 3 = 8.
Similarly, a = 5, b = a + 1 = 4.
y = 5 + 4 = 9.
Output :

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

  1. class static_out
  2. {
  3.     public static int x;
  4.     public  static int y;
  5.     public int add(int a, int b)
  6.     {
  7.         x = a + b;
  8.         y = x + b;
  9.         return 0;
  10.     }
  11. }
  12. class Program
  13. {
  14.     static void Main(string[] args)
  15.     {
  16.         static_out obj1 = new static_out();
  17.         static_out obj2 = new static_out();
  18.         int a = 2;
  19.         obj1.add(a, a + 1);
  20.         obj2.add(5, a);
  21.         Console.WriteLine(static_out.x + " " + static_out.y );
  22.         Console.ReadLine();
  23.     }
  24. }

a) 7 7
b) 6 6
c) 7 9
d) 9 7

Answer: c
Clarification: None.
Output :

9. Which of these access specifiers must be used for class so that it can be inherited by another subclass?
a) public
b) private
c) both public & private
d) none of the mentioned

Answer: a
Clarification: None.

10. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program
b) private members of class can only be accessed by other members of the class
c) private members of class can be inherited by a subclass, and become protected members in subclass
d) protected members of a class can be inherited by a subclass, and become private members of the subclass

Answer: c
Clarification: private members of a class cannot be inherited by a subclass.

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

  1.  class test
  2.  {
  3.      public   int a;
  4.      public  int b;
  5.      public  test(int i, int j)
  6.      {
  7.          a = i;
  8.          b = j;
  9.      }
  10.      public void meth(test o)
  11.      {
  12.          o.a *= 2;
  13.          o.b /= 2;
  14.      }
  15.  }
  16.  class Program
  17.  {
  18.      static void Main(string[] args)
  19.      {
  20.          test obj = new test(10, 20);
  21.          obj.meth(obj);
  22.          Console.WriteLine(obj.a + " " + obj.b);
  23.          Console.ReadLine();
  24.      }
  25.  }

a) 20, 40
b) 40, 20
c) 20, 10
d) 10, 20

Answer: c

12. Accessibility modifiers defined in a class are?
a) public, private, protected
b) public, internal, protected internal
c) public, private, internal, protected internal
d) public, private, protected, internal, protected internal

Answer: d

Leave a Reply

Your email address will not be published. Required fields are marked *