250+ TOP MCQs on Public & Private Access Modifier and Answers

C# multiple choice questions on public and private access modifiers in C# Programming Language.

1. Which of these is used as a default specifier for a member of the class if no access specifier is used for it?
a) private
b) public
c) public, within its own class
d) protected

Answer: a
Clarification: By definition if a class has no access specifiers, it defaults to private accessibility.

2. Which of these is used to access members of class before the object of that class is created?
a) public
b) private
c) static
d) protected

Answer: c
Clarification: None.

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

Answer: b
Clarification: None.

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

Answer: c
Clarification: By definition.

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

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

a) 48, 30
b) 48, 0
c) 0, 0
d) Compile time error

Answer: d
Clarification: variable ‘y’ is not accessible due to its access level.
Output :

Change private y to public y

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

  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

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

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

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

Answer: c
Clarification: t.sum(t) sends object ‘t’ as parameter whose variables a & b are multiplied and added by 2 respectively by sum() function of class math. Hence, a & b become 40 and 12 respectively.
Output :

8. Accessibility modifier 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
Clarification: None.

9. Choose the statements which are false in nature?
a) The base class member functions can access public member functions of derived class
b) An object of a derived class cannot access private member of the base class
c) Private members of the base class cannot be accessed by derived class member functions or objects of derived class
d) None of the mentioned

Answer: a
Clarification: None.

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

Answer: a
Clarification: By default main() is declared private if no other access specifier is used for it.

Leave a Comment

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

Scroll to Top