C# Access Modifiers MCQ

1. What does the 'public' access modifier do in C#?

a) Restricts access to the member within its declaring type
b) Allows access to the member from any other code
c) Allows access only within the same assembly
d) Provides global access to a variable

Answer:

b) Allows access to the member from any other code

Explanation:

The 'public' access modifier in C# makes a member accessible from any other code, regardless of assembly or class.

2. Which access modifier in C# is the most restrictive?

a) public
b) private
c) protected
d) internal

Answer:

b) private

Explanation:

The 'private' access modifier is the most restrictive, limiting access to the member within its declaring type only.

3. What is the default access modifier for a class in C#?

a) public
b) private
c) protected
d) internal

Answer:

d) internal

Explanation:

In C#, the default access modifier for classes is 'internal', meaning the class is accessible only within its own assembly.

4. What does the 'protected' access modifier do in C#?

a) Allows access from any class
b) Restricts access to within the class and derived classes
c) Restricts access to the same assembly
d) Allows access only within the same namespace

Answer:

b) Restricts access to within the class and derived classes

Explanation:

The 'protected' access modifier allows access to a member from within its declaring type and any derived classes.

5. How does the 'internal' access modifier work in C#?

a) Restricts access to within the current class
b) Allows access from any class within the same assembly
c) Allows access only from derived classes
d) Provides unrestricted access to the member

Answer:

b) Allows access from any class within the same assembly

Explanation:

The 'internal' access modifier in C# makes a member accessible only within the same assembly.

6. What is the combination of 'protected' and 'internal' called in C#?

a) protected internal
b) internal protected
c) private public
d) public private

Answer:

a) protected internal

Explanation:

The 'protected internal' access modifier in C# allows access to a member from within its declaring type, derived types, or any type in the same assembly.

7. What access modifier should be used to allow a member to be accessible only within its declaring type?

a) public
b) private
c) protected
d) internal

Answer:

b) private

Explanation:

The 'private' access modifier restricts access to a member exclusively within its declaring type.

8. Which access modifier allows a member to be accessed from derived classes, even in different assemblies?

a) private
b) internal
c) protected internal
d) public

Answer:

c) protected internal

Explanation:

The 'protected internal' access modifier allows a member to be accessible from derived classes, even if they are in different assemblies.

9. What is the default access modifier for a method in C#?

a) public
b) private
c) protected
d) internal

Answer:

b) private

Explanation:

In C#, the default access modifier for methods is 'private' unless specified otherwise.

10. Can a 'private' member of a class in C# be accessed from an instance of that class?

a) Yes, from anywhere
b) No, never
c) Yes, but only within the class itself
d) Yes, but only from derived classes

Answer:

c) Yes, but only within the class itself

Explanation:

Private members of a class are accessible from within the class itself but not from outside it, including from instances of the class.

11. What is the effect of the 'protected internal' access modifier on a member in C#?

a) Accessible only within the same assembly and from derived classes in other assemblies
b) Accessible from anywhere
c) Accessible only from derived classes
d) Accessible only within the same class

Answer:

a) Accessible only within the same assembly and from derived classes in other assemblies

Explanation:

The 'protected internal' access modifier allows a member to be accessed from within its own assembly and from derived classes, even in other assemblies.

12. Which access modifier should be used for members that should be accessible only to derived classes and classes within the same assembly?

a) private
b) protected
c) internal
d) protected internal

Answer:

d) protected internal

Explanation:

The 'protected internal' access modifier allows access to a member by derived classes and any class within the same assembly.

13. What does the 'public' access modifier indicate about a member's accessibility?

a) Restricted within the class
b) Restricted within the assembly
c) Accessible from any other code
d) Accessible only within derived classes

Answer:

c) Accessible from any other code

Explanation:

Members declared as 'public' are accessible from any other code, making them the least restrictive in terms of access control.

14. If a member is declared without any access modifier, which access level is applied by default in C#?

a) public
b) private
c) internal
d) protected

Answer:

b) private

Explanation:

In C#, if no access modifier is specified for a member, it is implicitly 'private'.

15. What is the purpose of using different access modifiers in C#?

a) To optimize code performance
b) To control the scope of access to class members for encapsulation and security
c) To define different types of classes
d) To specify the return type of methods

Answer:

b) To control the scope of access to class members for encapsulation and security

Explanation:

Access modifiers in C# are used to control the visibility and accessibility of class members, which is essential for encapsulation and security in object-oriented programming.

Leave a Comment

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

Scroll to Top