C# Questions & Answers – Accessor controls of class

This set of C# Assessment Questions and Answers focuses 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
View Answer

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

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

Answer: c
Explanation: None.
advertisement
advertisement

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

Answer: b
Explanation: None.

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

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

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

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

Answer: b
Explanation: None.
advertisement

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

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

Answer: c
Explanation: None.
Output :

 7, 9

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

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

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

Answer: c
Explanation: None.
Output :

20, 10

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

Answer: d
Explanation: None.

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# Assessment Questions, 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.