C# Questions & Answers – Public & Private Access Modifier

This section of our 1000+ C# multiple choice questions focuses 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: None.

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

Answer: b
Explanation: None.
advertisement
advertisement

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
View Answer

Answer: c
Explanation: By definition.

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

Note: Join free Sanfoundry classes at Telegram or Youtube
  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
View Answer

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

advertisement
Change private y to public y

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

advertisement
  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
View Answer

Answer: b
Explanation: 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 :

5, 9

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
View Answer

Answer: c
Explanation: 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 :

 40, 12

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
View Answer

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

Answer: a
Explanation: None.

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

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

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

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.